Installing Node.js on Windows 8

Node comes with an installer, but I hit a couple of bugs that knackered my initial attempts to call either node or npm.

I followed the installation instructions trying to run a command for the first time and got 'node' is not recognized as an internal or external command. For anyone else with the same issue here’s what I stumbled through.

I’m running Windows 8.1 (64-bit) with Node v0.10.32.

  1. Run the .msi installer
  2. This installs it to C:\Program Files\nodejs

In that directory is node.exe for running node commands and npm.bat for installing node modules.

Environment variables

The simplest solution for this is just to do a restart, but for thos who don’t want to aka me…

The installer doesn’t seem to install the environment variables properly (at least it didn’t when I was using windows 8).

So although if you edit the Environment variables you will see that C:\Program Files\nodejs in the path, if you go into a command prompt and type echo %PATH% it doesn’t appear.

I’m not sure why – but I had to make a minor modification to the PATH, save it and then put the modification back for the nodejs to show up in the path.

Then you can run either node or npm from anywhere.

Get the home page server example.js working

Here’s the code just in case something changes:

var http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');

For some reason even though C:\Program Files\nodejs had been added to the system PATH it wasn’t showing up in my path when running dos commands – it took editing the path and saving it to suddenly show up the nodejs in the path.

Once you’ve got the path set up properly then you just have to create the example.js file and run the following commands from a windows command prompt:

> cd C:\path\to\node\example
> node example.js

Getting npm working

  1. There is a npm.bat command in C:\Program Files\nodejs
  2. However there’s a known npm issue that requires that the %APPDATA%\npm directory has been created manually otherwise npm doesn’t work
  3. You’ll get an error along the lines Error: ENOENT, stat 'C:\Users\[username]\AppData\Roaming\npm'
  4. So you have to create your own %APPDATA%\npm

N.B. For “dummies”, aka me: where ever you run npm install in the windows command prompt it will create a node_modules directory and install the module under there
e.g.

C:\Users\[username]> npm install grunt

will install grunt into C:\Users\[username]\node_modules\grunt not into %APPDATA% or C:\Program Files\nodejs\node_modules

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s