how to install nodejs and npm on ubuntu

    Posted in Linux Servers on Aug 22, 2019

    Add Nodejs Package on Server

    We are going to add the latest package on server so that we can install latest nodejs

    sudo apt-get install curl
    curl -sL https://deb.nodesource.com/setup_12.x | sudo -E bash -

    Install Node.js on Ubuntu

    sudo apt-get install nodejs

    Check Node.js Version on server

    node -v 
    
    v12.8.0

    Check NPM Version on server

    npm -v 
    
    6.10.2

    Node and NPm is installed sucessfully !! Now we are going to create demo node js server

    vim server.js

    After creating a file, please add the following content :

    var http = require('http');
    http.createServer(function (req, res) {
    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.end('Hello Node Server\n');
    }).listen(5000, "127.0.0.1");
    console.log('Server running at http://127.0.0.1:5000/');

    Please start the Node application using following command:

    node server.js

    After that you will see the output

    debugger listening on port 5858
    Server running at http://127.0.0.1:5000/

    Note : if you want to set node js on public ip of server you can repalce the ip 127.0.0.1 to your server ip . Also need to open the define port in server firewall.