This is the 1st in series of posts leading up to the 3rd annual Node.js Knockout about how to use node.js.
This post covers how to install node and npm on three popular development platforms: Mac, Ubuntu, and Windows.
Instructions for other platforms can be found on the Node Wiki.
The Easy Way
We personally use package managers to make it easier to keep our node installation up to date, so that’s the approach we generally describe below.
However if you want to skip the steps and download pre-built binaries, you can do so at Joyent’s site: download a binary.
Mac
- Install Xcode. Note: do this before the competition, as the Xcode download can take hours.
- Install Homebrew.
- At the terminal, type:
brew install node.
That’s it! Check it worked with a simple Hello, World! example.
Ubuntu
On the latest version of Ubuntu, you can simply:
sudo apt-get install nodejs nodejs-dev npm
On earlier versions, you might need to update your repository:
sudo apt-get install python-software-properties
sudo add-apt-repository ppa:chris-lea/node.js
sudo apt-get update
sudo apt-get install nodejs nodejs-dev npm
Then, check it worked with a simple Hello, World! example.
Windows
Since Windows package managers are less common, we recommend just downloading the Windows binary.
Hello, Node.js
Here’s a quick program to make sure everything is up and running correctly.
// hello_node.js
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello Node.js\n');
}).listen(8124, "127.0.0.1");
console.log('Server running at http://127.0.0.1:8124/');
Run the command by typing node hello_node.js in your terminal.
Now, if you navigate to http://127.0.0.1:8124/ in your browser, you should see the message.
Congrats
You’ve installed node.js and npm.