Linux: How to install Node.js on Debian 8

…and start using it

Note: This tutorial will most likely also work on Ubuntu since it's based on Debian.

While it is possible to install Node.js with he Advanced Packaging Tool (apt-get), or even with NVM, it is not recommended because they lag behind in versioning, and because those packages do some funky things with the naming conventions. Instead it's much better to install it manually.

Here's how!

Go to the main site of Node.js: http://www.nodejs.org

Download the version you want. LTS works fine in most cases. It's stable and secure, and you don't need to update it as often – but it might not be as cutting edge as the other.

Open your trusty terminal and cd into your home Downloads folder and untar the file, like so:

$ tar -xf [file]

Yes, you can also open the fild browser and just double click the file on most graphical shells, but we're trying to look smart and nerdy here!

cd into the unpacked folder, and move the files in the /bin folder to /usr/local/bin, like so:

$ sudo mv [node-folder]/bin/* /usr/local/bin

However it seems like the node executable doesn't work from /usr/local/bin, but this can easily be fixed with a symlink to it, like so:

$ ln -s /usr/local/bin/node /usr/bin/node

Then move the contents of the /lib folder to /usr/local/lib/, like so:

$ sudo mv [node-folder]/lib/node_modules/ /usr/local/lib/

When that is done, you're ready to test, so try the following:

$ node --version
$ npm --version

Both should return a simple version number.

Also check NPM's global install path like so:

$ npm root -g

It should be /usr/local/lib/node_modules.

For simple upgrading and version control, use NPM to install n:

$ npm cache clean
$ npm install -g n

Then upgrade Node.js by running the following:

$ n stable

To see options, just do:

$ n

Where to store your Node.js projects

For sanity, it's usually smart to make a folder simply called node (with small caps for ease of use) that resides in your home user directory. Make it the only place you start new Node-projects, lest you have to go looking for them all over the place.

How to start a new project

Make a new project folder in your home node directory. cd into it, and initialize the project with npm init, like so:

$ mkdir -p ~/node/project
$ cd ~/node/project
$ npm init

This will make a file called package.json which holds the dependencies for your projects.

Whenever you need to install a new Node module, like Express, make sure you're cd'd into the right project folder and do the following (with Express used as the example):

$ npm install express --save

The syntax is thus:

$ npm install [module] --save

The --save argument makes sure package.json is updated with the new dependency, whether it is Epxress or another module. The only exception is if you want to install a global module, like nodemon (which restarts and logs node while developing). In that case do the following (with nodemon as an expample):

$ npm install -g nodemon

The syntax is thus:

$ npm install -g [module]

Removing Node.js

First remove the executables:

$ sudo rm /usr/local/bin/{node,npm}

Then remove the symlink:

$ sudo rm /usr/bin/node

And lastly remove the global modules:

$ sudo rm -rf /usr/local/lib/node_modules

You may also want to delete local modules. While it's probably wise to keep your local projects backed up, you can still go ahead and delete the node_modules folder for each of the projects you're not using anymore. Sometimes that can actually save a lot of space, especially if you like to work and test multiple project at the same time.

The neat thing here is that if you need to run a project you already deleted the node_modules folder from, you can get it all back by using this simple command:

$ npm install

By that command NPM will look into package.json and re-install all dependencies so you can run it again. Pretty neat!

Where to learn JavaScript

You'll have to learn JavaScript on your own, though. A very nice place to start, however, is Codecademy: https://www.codecademy.com/learn/javascript

And then there is Eloquent JavaScript: http://eloquentjavascript.net/. Highly recommended! You can even download the source and compile it for Kindle or other E-book readers with Calibre.