Are you convinced? Are you going to start using Node.js?
Good! Let’s not wait around and get on with installing the little critter!
In this chapter, we will cover the installation process for every OS:
Mac OS X
You only need to read the part that corresponds to your operating system. There won’t be anything difficult, don’t worry.
Once you've installed Node.js on your computer, we will create a simple application to make sure the installation process went well. Obviously, you should all read this last section to be ready for the next chapter. ;)
Installing Node.js in Windows
To install Node.js in Windows, all you need to do is download the installer available on the Node.js website. Just click on the Install link.
You can also go to the downloads page for more options (see next figure).
You can either download the .msi or the .exe (the result will be the same). Take the 64 bits version if you have a 64 bits version of Windows (most up to date PCs do).
If in doubt, go with the 32 bits version.
Then launch the installer (see next figure).
After a few standard screens, you will be asked what you want to install. I recommend that you leave all the boxes checked (see next figure).
The installation then takes place. It only takes a few seconds!
You will be told when Node.js has been installed. But where? How does it work?
Basically, you should have 2 programs installed:
Node.js command prompt: it’s a Windows console configured to recognize Node.js. This is where you’ll be launching your Node.js programs and is therefore what we’ll be using most of the time (see next figures).
Installation of Node.js in Mac OS X
If you are using Mac OS X, you can click on the "Install" link using the homepage of the Node.js website.
If you would like more options, go to the download page.
The best option is to choose the installer (.pkg file). It opens an installation assistant (see next figure) where you just click like crazy on "Continue", "Continue", "Continue", "Finish".
Once the installation has finished, you can check that Node.js is working correctly by typing thenodecommand into the console. Open a Terminal window (the Terminal is installed by default under Mac OS X), by going to the Finder, in the "Applications" section, and selecting "Terminal". I would advise you to put a shortcut to it in the dock!
Type in a few commands such as
node -v (to get the number of the version) or just
node to launch the interactive interpreter (see next figure).
To quit the interpreter, press ctrl + D (it’s the classic command used to exit interpreters under Linux and Mac).
Installation of Node.js in Linux
As usual in Linux, you have two choices:
The warrior method, that involves downloading the sources and compiling them.
The soft method, which involves using the distributor’s packet manager.
It’s a question of taste, here. I have nothing against the warrior method, but because I generally have fewer problems using the soft method, I prefer to go via a packet manager.
In Ubuntu for example, you should enter the following commands:
sudo apt-get install python-software-properties python g++ make sudo add-apt-repository ppa:chris-lea/node.js sudo apt-get update sudo apt-get install nodejs
To have the add-apt-repository command you may need to download software-properties-common:
sudo apt-get install software-properties-common
And there you go, job done! To check that Node has been installed properly, type in a few commands in the console such as
node -v or just
The first shows the version number of Node.js that you’ve installed.
Don’t worry, we won’t be writing our programs in the interactive interpreter. We’ll create .js files instead and ask Node to run them.
Testing Node.js with a minimal program
Now it’s time to check that Node.js is working properly. First, we’ll write a little program that displays messages in the console. It will give us a chance to see how the running of .js files works using Node.
console.log ('Welcome to Node.js!');
Save your file under the extension .js. For example, test.js.
Then, open a console in the folder where the test file is and enter the command :
You should have the following result in the console:
$ node test.js Welcome to Node.js!
Well done! You have created your first Node.js program! :D
It couldn’t have been any easier, could it? We just asked to write a message in the console.
You have seen that to launch the Node.js program, you just needed to indicate the name of the .js file to be run. You know everything that you need to know for the time being!
In the next chapter, we’ll start working on the serious stuff: we’re going to create our first Node.js app. Watch out, it’s going to get more complicated!
Installing Node.js is simple, whether it’s in Windows, Mac, or Linux.
A Node.js app is launched with the command node filename.js.