DailyJS

Node Tutorial Part 15: Node Versions, npm Publishing

Alex R. Young

Subscribe

@dailyjs

Facebook

Google+

tutorials server node lmawa nodepad npm

Node Tutorial Part 15: Node Versions, npm Publishing

Posted by Alex R. Young on .
Featured

tutorials server node lmawa nodepad npm

Node Tutorial Part 15: Node Versions, npm Publishing

Posted by Alex R. Young on .

Welcome to part 15 of Let's Make a Web App, a tutorial series about
building a web app with Node. This series will
walk you through the major areas you'll need to face when building your
own applications. These tutorials are tagged with
lmawa.

Previous tutorials:

Switching Node Versions

People are starting to send in error reports when they try to run
Nodepad with Node 0.4.x. I'll migrate it to support that version soon,
but before that we have a few loose ends to tie up. However, I realise
that waiting a few weeks for me to catch up is annoying, so in the
meantime I'll demonstrate how to easily switch between versions of Node.

I'm going to use n to do this. It
can be installed with:

npm install n

I still have Node 0.2.4 on my desktop where I write DailyJS articles.
That version is pretty old now. Let's switch to 0.2.6 using n:

n 0.2.6

It might take a while to build Node on your system, just about long
enough to make a coffee.

Now node -v should show v0.2.6.

Publishing Packages with npm

jamesm-sitegen asked us if we could publish Nodepad to npm. It's actually very easy to do this, so easy it isn't even worth a full
tutorial.

The key thing to know is npm comes with lots of man pages. If you'd like
to work with npm and publish packages, npm-developers is
like a getting started guide. Type:

man npm-developers

to read it. Another useful man page is npm-adduser which
tells you how to register yourself with npm. The npm
adduser
command can be used to create a new account to publish
packages, or to simply use existing credentials to add another computer.
Just type:

npm adduser

and follow the prompts. The email address and other details will be
stored in ~/.npmrc.

Next you need to write a package.json which fortunately
isn't too hard. You could copy an existing one from another project like
Nodepad, but make sure you type man npm-json and learn the
basics for yourself first.

Once you've got a file you're happy with you can do a local dry-run
before publishing the package:

npm install .

Publishing for real is easy. Run this from your project:

npm publish

To confirm it was published, I typed this:

npm ls nodepad

And I saw the following (notice the remote flag):

nodepad@0.0.1   A notepad written with Node    =alexyoung installed latest remote

Making Binaries

Even better, we can make a little wrapper to allow people to launch
Nodepad. I've added this to bin/nodepad.js:

#!/usr/bin/env node
var app = require('../app.js');
app.listen(3000);

And then in package.json I've got this:

"bin": {
  "nodepad": "bin/nodepad.js"
}

The launcher script just requires our Express app -- npm will wrap
everything so the paths make sense. When writing binaries, use paths
relative to bin/.

Conclusion

When following random tutorials written by gnarly old programmers on the
Internet, it's a good idea to pay attention to the version of everything
in your environment. If your scripting language isn't the same version,
the tutorial probably won't make sense. Tools like n make it easy for
us Node programmers to switch between versions of Node without any fuss.

And publishing with npm is easy, you don't even need to leave your
shell.

The latest commit is
89abea1