Change Node.js version to specific version

Many times we have some dependency which forces us to use some specific Node.js version. Specific Node.js version can be installed as below:

Step#1

  • clear the cache by sing following command
$ sudo npm cache clean -f

Step#2

  • Use following command to install node globally and specify the required version.
$ sudo npm install -g n
$ sudo n 6.9.5

You will receive following confirmation of Node.js installation:

User@hostname:~$ sudo n 6.9.5

     install : node-v6.9.5
       mkdir : /usr/local/n/versions/node/6.9.5
       fetch : https://nodejs.org/dist/v6.9.5/node-v6.9.5-linux-x64.tar.gz
######################################################################## 100.0%
   installed : v6.9.5

Alternatively, you can download required Node.js version from Node.js repository http://nodejs.org/dist/

Use following command to download required version file.

$ wget nodejs.org/dist/v0.10.36/node-v0.10.36-linux-x64.tar.gz

Same package can be downloaded without command line utility as well. install the this compressed file to get your required Node.js version.

Getting started with Node.js on Digital Ocean

I have evaluated and used multiple hosting options, PaaS like openshift, Heroku and Iaas like Digital Ocean, Linode and Amazon EC2 and finally have zeroed on Digital Ocean for their simplicity, cost effectiveness and most importantly availability of rich documentations and tutorials.

Pros and Cons of PaaS IaaS has been discussed in many places so we will not go into that discussion here. This is quick refence guide for getting started with Node.js on Digital Ocean.

Digital Ocean has rich set of tutorials for getting stated with different stacks (LAMP, MEAN, crude Node.js etc). Here are set of tutorials which you can follow to get started with Node.js.

  • Get started with Initial Server Set up with Ubuntu 16.04 LTS. These steps are self explainatory and does not need any further clarification.

    • Step One — Root Login
    • Step Two — Create a New User
    • Step Three — Root Privileges
    • Step Four — Add Public Key Authentication
    • Step Five — Disable Password Authentication
    • Step Six — Test Log In
    • Step Seven — Set Up a Basic Firewall
  • Install Node.js. This step will install latest Node.js. Some times you might need some specific version of Node.js. For example, Ghost blogging platform wont run of latest Node.js version as its not yet supported. Ghost official website recommendeds >=4.2 <5.* (Node v4 argon LTS). Please check this tutorial if you want to install specific version of Node.js.

  • Now basic server set up is done and Node.js is installed, now you need nginx server. Follow how to install nginx tutorial to install ngnix.

  • Now you are all set. Follow how to set up Node.js tutorial to set up very basic Node.js application. Once you have done with this step, to get more comfortable with Node.js on ubuntu server, follow creating you first Node.js tutorial . This will give you more hands on and will increase your comfort level.

  • Setting up domain on Digital Ocea is super simple. Simple follow this tutorial to get it done.

    I faced an issue with domains. My website was working perfectly wine with https://example.com but it was not working for https://www.example.com. If you also face this issue, please go to Droplets->Add Domain–>Manage Domains and add a ‘A’ record as ‘www’ (without quotes), select your droplet and click on create record. This will resolve your issue.

  • (optional) You can make your website secure by following this tutorial.

  • (optional) If you are getting started and would like to host multiple Node.js websites on the single droplet, follow how to set up multiple virtual blocks tutorial.

Installing Node.js and NPM on Ubuntu 16.04 LTS

Node.js is a JavaScript runtime built on Chrome’s V8 JavaScript engine. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient. Node.js’ package ecosystem, npm, is the largest ecosystem of open source libraries in the world.

In this article we will look at how to install nodejs and npm on ubuntu 16.04 LTS.

Step#1
Following commands can be used to check if nodejs and npm is installed on your machin. If its already installed,you will get the installed versions.

$ npm -v
$ node -v

Step#2
If Node.js is not installed, we can install it by using Node.js PPA provided by official site. As a prerequisite, we need to install python-software-properties package if not installed already.

$ sudo apt-get install python-software-properties
$ curl -sL https://deb.nodesource.com/setup_7.x | sudo -E bash -

Above command will install Node.js versoon 7.X. if you need any previous version, lets say Node v4.8.0 (LTS), please use following command.

$ curl -sL https://deb.nodesource.com/setup_4.x | sudo -E bash -

Step#3
After adding PPA files, your required Node.js version can be installed using following command.

$ sudo apt-get install nodejs

Once installation is completed, versions can be checked using following commands

$ npm -v
$ node -v

These commands will let you know the latest versions.

Once Node.js has been installed, please check article, creating your first Node.js application to get started

Creating Your First Node.js Application

Follow these instructions to create your fist nodejs application is less than 5 mins.

I assumed you already have nodejs and npm installed on your machine, if not please follow “How to install nodejs” tutorial and come back here after installation is completed.

Step#1
Open command promt in admin mode and go to the folder where you want to create your first nodejs project. Install express and express-generator using following commands.

npm install express –save  
npm install express-generator –save`  

Step#2
There are multiple templating option that you can choose such as handlebars, pug, hogan but I choose ejs as its very user friendly and it does not have steep learning curve. Create your first nodejs app by using following command

express --view=ejs myapp  

Step#3.
cd into new folder and install node packages by using following commands

cd . && npm install  

Step#4
And you are all set. Start your nodejs application by using following command. This will start your application in debug mode.

SET DEBUG=myapp:* & npm start  

If you don’t want to start application in debug mode you can just simple start Node.js app using following command

npm start  

This will get you started with nodejs under 5 minutes. This will create your bare minimum nodejs application. now you can go ahead and do any further front end development that you need.

Our nodejs application is just like steel frame, it just send you one simple webpage, nothing else, we can enhance it further to create data feed api and other complex features. We will look at step by step guide of these at later stages.