JavaScript / nodejs

How to set up Nodejs for development and production environments on Ubuntu 16.04

Node.js is a Javascript platform for server-side programming that allows users to build network applications quickly. By leveraging Javascript on both the front-end and the back-end, development can be more consistent and be designed within the same system.

NPM: makes it easy to share and reuse Javascript code. The bits of reusable code are called packages or modules. A package is a directory with one or more files in it which has a file called ‘package.json’ which is the meta-data of the package. Nodejs comes with NPM installed.

The following steps would help to install nodejs on a development machine.

To install nodejs directly on the Ubuntu using the debian packages

sudo apt-get update
sudo apt-get install nodejs
locate nodejs
whereis nodejs

To uninstall or remove nodejs in Ubuntu

dpkg -l
dpkg purge nodejs
dpkg  –remove

dpkg -l nod*

sudo apt-get purge nodejs
sudo apt-get purge –auto-remove nodejs
sudo apt-get purge nodejs
sudo apt-get autoremove

Though it is a quick way to install nodejs using package on Ubuntu, but not the preferred one, to have a more controlled installation where in multiple versions of nodejs can co-exist, it is better to install in the opt directory using the tar files.

Two approaches which can be thought off during deployment

app: nodejs application and runtime would be there on one server, and the end users would be using this server IP address to access the nodejs application

web:NGINX web server which would serve as front end for end users to make requests. This server would act as a reverse proxy to the nodejs application. End users would access this NGINX IP Address to access the nodejs application

If only one server is preferred, for the remaining part of the tutorial just replace the IP Address with localhost/127.0.0.1

A simple deployment illustration of nodejs where in the web server could be a NGINX web server which would serve as front end for end users to make requests. This server would act as a reverse proxy to the nodejs application. End users would access this NGINX IP Address to access the nodejs application.

 

Reverse Proxy to Node.js Application

Download the latest nodejs from for Linux

user@LT-201:~/Downloads$ wget https://nodejs.org/dist/v6.10.0/node-v6.10.0-linux-x64.tar.xz
–2017-03-05 13:59:50–  https://nodejs.org/dist/v6.10.0/node-v6.10.0-linux-x64.tar.xz
Resolving nodejs.org (nodejs.org)… 104.20.22.46, 104.20.23.46, 2400:cb00:2048:1::6814:162e, …
Connecting to nodejs.org (nodejs.org)|104.20.22.46|:443… connected.
HTTP request sent, awaiting response… 200 OK
Length: 9376372 (8.9M) [application/x-xz]
Saving to: ‘node-v6.10.0-linux-x64.tar.xz’

node-v6.10.0-linux- 100%[===================>]   8.94M  3.84MB/s    in 2.3s

2017-03-05 13:59:54 (3.84 MB/s) – ‘node-v6.10.0-linux-x64.tar.xz’ saved [9376372/9376372]

Create a directory to hold contents of the extracted archiv

user@LT-201:~/Downloads$ mkdir node
user@LT-201:~/Downloads$ tar xvf node-v*.tar.?z –strip-components=1 -C ./node //-C option is to Change Directory before performing any operation. –strip-components=1, specifies the number to strip components from files names on extraction.

Next, we’ll configure the global prefix of npm, where npm will create symbolic links to installed Node packages, to somewhere that it’s in your default path. We’ll set it to /usr/local with this command:

user@LT-201:~/Downloads$ mkdir node/etc //this creates a directory by name etc under node directory
user@LT-201:~/Downloads$ echo ‘prefix=/usr/local’ > node/etc/npmrc //this creates a a global prefix for npm, a file with name npmrc with global config

With all nodejs extracted, now we can move the nodejs and npm binaries to our installation location /opt/node with the following command

user@LT-201:~/Downloads$ sudo mv node /opt/

Make root as the owner of the files

user@LT-201:/opt$ sudo chown -R root: /opt/node

Then we move to create symbolic links of the node and npm binaries in the default path. We will put the links in /usr/local/bin with these commands

user@LT-201:/opt$ sudo ln -s /opt/node/bin/node /usr/local/bin/node
user@LT-201:/opt$ sudo ln -s /opt/node/bin/npm /usr/local/bin/npm

Verify the node is installed by checking

user@LT-201:/opt$ node -v
v6.10.0

The Node.js runtime is now installed, and ready to run an application! Let’s write a Node.js application.

Refer the next article in the series to create your first application

 

Leave a Reply

Your email address will not be published. Required fields are marked *