Node.js is a JavaScript platform for general programming that allows users to create network applications quickly. By leveraging JavaScript in both frontend and backend, Node.js makes development more uniform and integrated. Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine. runtime for easily building fast, scalable network applications. In this guide, we'll show you how to get started with Node.js on an Ubuntu 18.04 server.
You can add the PPA to your Ubuntu 18.04 LTS systems and install node.js on Linux VPS with a few easy commands. To start the process you should have a non-root user account with sudo privileges set up on your system
Add Node.js PPA from NodeSource
Ubuntu 18.04 contains a version of Node.js in its default repositories that can be used to provide a consistent experience across multiple systems. You can select which version you want to install on the system. Add the following PPA's to your system to install Nodejs on Ubuntu.
To get a more recent version of Node.js you can add the PPA (personal package archive) maintained by NodeSource.
sudo apt-get install curl curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash -
Or you can use the most recent LTS release available.
sudo apt-get install curl curl -sL https://deb.nodesource.com/setup_12.x | sudo -E bash -
Install the package Version from Ubuntu repositories or from NodeSource PPA
To get this version, you can use the apt package manager. Refresh your local package index by typing:
sudo apt update sudo apt install nodejs
If you also want to install npm, the Node.js package manager. You can do this by typing:
sudo apt install npm This will allow you to install modules and packages to use with Node.js.
After installing node.js verify and check the installed version.
node -v v14.2.0
Check the npm version
npm -v 8.10.7
Create a Demo Test Web Server
If you want to test your node.js install you can create a web server with “Hello World!” text. Create a file server.js
nano test-server.js
and add the following content
test-server.jsvar http = require('http'); http.createServer(function (request, response) { response.writeHead(200, { 'Content-Type': 'text/plain' }); response.end('Hello World from Noden'); }).listen(1333, "127.0.0.1"); console.log('Server running at http://127.0.0.1:3000/');
Now start the Node application using the command.
node test-server.js debugger listening on port 5858 Server running at http://127.0.0.1:1333/
You can also start the application with debugging enabled with the following commands.
node --inspect test-server.js Debugger listening on ws://127.0.0.1:9229/428cf97a-a4b6-3158-134b-c1357c9b24c54 For help, see: https://nodejs.org/en/docs/inspector Server running at http://127.0.0.1:1333/
The web server has been started on port 1333. You can access it from http://127.0.0.1:1333/ URL in browser. Now you will need to configure a front-end server for your app.
Removing Node.js
You can uninstall Node.js using apt, to remove the distro-stable version, you will need to work with the apt utility at the system level.
To remove the distro-stable version, type the following:
sudo apt remove nodejs
This command will remove the package and retain the configuration files. These may be of use to you if you intend to install the package again at a later point. If you don’t want to save the configuration files for later use, then run the following:
sudo apt purge nodejs
This will uninstall the package and remove the configuration files associated with it.
As a final step, you can remove any unused packages that were automatically installed with the removed package:
sudo apt autoremove