• 15 heures
  • Difficile

Mis à jour le 17/12/2019

Installing Node.js

Connectez-vous ou inscrivez-vous gratuitement pour bénéficier de toutes les fonctionnalités de ce cours !

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:

  • Windows

  • Mac OS X

  • Linux

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).

The Node.js download page
The Node.js download page

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).

Installing Node.js
Installing Node.js

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).

Choice of features to install
Choice of features to install

The installation then takes place. It only takes a few seconds!

The installation is finished!
The installation is finished!

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: it’s the Node.js command interpreter (which we will talk about at the end of this chapter). We won’t be using it much in practice. It’s used to test JavaScript commands.

  • 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).

Node.js - The Node.js interpreter in Windows (not used much)
Node.js - The Node.js interpreter in Windows (not used much)
Node.js command prompt - the Node.js console (used regularly)
Node.js command prompt - the Node.js console (used regularly)

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 Node.js download page
The Node.js 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".

Installing Node.js in Mac OS X
Installing Node.js in Mac OS X

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 asnode -v  (to get the number of the version) or justnode  to launch the interactive interpreter (see next figure).

Running Node.js in the Terminal
Running Node.js in the Terminal

When the interpreter is launched, you can type in your JavaScript commands and get an answer. Here, for example, I asked how much 1 plus 1 comes to. :-°

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 asnode -v  or justnode .

The first shows the version number of Node.js that you’ve installed.
The second launches the interactive interpreter of Node.js. You can type JavaScript code into it (try it out by typing "1+1" to see how it works). To exit the interpreter, press Ctrl + D.

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.

To start with, open your favorite text editor (vim, Emacs, Sublime Text, Notepad++, etc.) and enter the following JavaScript code:

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 :

node test.js

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!

Summing up

  • Installing Node.js is simple, whether it’s in Windows, Mac, or Linux. 

  • A Node.js app is written in JavaScript under the .js extension.

  • A Node.js app is launched with the command node filename.js. 

Exemple de certificat de réussite
Exemple de certificat de réussite