Linux Tactic

Mastering Nodejs on Linux: Installation Removal and Usage

Node.js is an open-source, cross-platform server-side JavaScript runtime environment that allows developers to build scalable, high-performance web applications. In this article, we will discuss the installation of Node.js 12 on Linux and how to create a simple file on the server and run a Node.js server.

Installation of Node.js 12 on Linux:

Before installing Node.js, we need to install its dependencies. These dependencies include the build-essential package, which contains the necessary tools to compile and install Node.js from source.

To install these dependencies on Ubuntu, we can use the apt package manager by running the following command:

“`

sudo apt update

sudo apt install build-essential

“`

Once we have installed the dependencies, we can proceed with the installation of Node.js 12. We can either install it from the official Node.js website or use a Node.js package manager like nvm.

In this article, we will use the latter method. To install nvm on Linux, we can use the curl command by running the following command:

“`

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh | bash

“`

Once we have installed nvm, we can install Node.js 12 by running the following commands:

“`

nvm install 12

nvm use 12

“`

This will install Node.js 12 and set it as the default version. Creating a file on the server:

Now that we have installed Node.js 12, let’s move on to creating a simple file on the server.

To create a file, we can use the touch command by running the following command:

“`

touch index.js

“`

This will create a file named index.js in the current directory. Next, let’s open the file in a text editor and add some code.

We can use the nano text editor by running the following command:

“`

nano index.js

“`

This will open the index.js file in the nano text editor. Let’s add the following code:

“`

const http = require(‘http’);

const hostname = ‘127.0.0.1’;

const port = 3000;

const server = http.createServer((req, res) => {

res.statusCode = 200;

res.setHeader(‘Content-Type’, ‘text/plain’);

res.end(‘Hello World!’);

});

server.listen(port, hostname, () => {

console.log(`Server running at http://${hostname}:${port}/`);

});

“`

Save the file by pressing Ctrl + X, followed by Y, and then Enter.

Running Node.js server and accessing the file:

Now that we have created the file, let’s run a Node.js server and access the file. To run the server, we can use the node command by running the following command:

“`

node index.js

“`

This will start the server and output the following message in the terminal:

“`

Server running at http://127.0.0.1:3000/

“`

Now, let’s open a web browser and navigate to http://127.0.0.1:3000/.

This will display a “Hello World!” message on the page. Conclusion:

In this article, we discussed the installation of Node.js 12 on Linux and how to create a simple file on the server and run a Node.js server.

By following these steps, developers can begin building scalable, high-performance web applications using Node.js. 3: Removing Node.js on Linux

Node.js is a powerful tool for building web applications on Linux, but sometimes you may need to remove it for various reasons.

In this subtopic, we’ll look at how to remove Node.js on Linux using apt. Node.js removal using apt:

The apt package manager is the default package manager for Ubuntu and other Linux distros that are based on it, such as Linux Mint.

To remove Node.js using apt, we can run the following command:

“`

sudo apt remove nodejs

“`

This will remove the Node.js package along with its dependencies. If you want to remove all of the configuration files associated with Node.js, you can use the purge option like this:

“`

sudo apt purge nodejs

“`

This will remove the Node.js package along with its dependencies and configuration files. If you also installed Node.js using the nvm package manager, you may need to remove it separately using the following command:

“`

nvm uninstall node

“`

This will uninstall the current version of Node.js that is being used by nvm. After removing Node.js, you can confirm that it has been uninstalled by running the following command:

“`

node -v

“`

If Node.js has been removed successfully, this command should output “command not found”. 4: Installation and use of Node.js on Linux Mint

Now that we have discussed how to install and remove Node.js on Linux, let’s take a look at how to use Node.js to build web applications on Linux Mint.

Installation of Node.js on Linux Mint:

To install Node.js 12 on Linux Mint, we can use the apt package manager by running the following commands:

“`

sudo apt update

sudo apt install nodejs

“`

This will install Node.js version 12 by default. To verify that Node.js has been installed correctly, we can run the following command:

“`

node -v

“`

This should output the version number of the installed Node.js. Creating a simple web application using Node.js on Linux Mint:

Let’s create a simple web application using Node.js on Linux Mint.

To do this, let’s create a new file called index.js and add the following code:

“`

const http = require(‘http’);

const hostname = ‘127.0.0.1’;

const port = 3000;

const server = http.createServer((req, res) => {

res.statusCode = 200;

res.setHeader(‘Content-Type’, ‘text/plain’);

res.end(‘Hello World!’);

});

server.listen(port, hostname, () => {

console.log(`Server running at http://${hostname}:${port}/`);

});

“`

This code creates a web server that listens on port 3000 and sends a “Hello World!” response to any incoming requests. To run this application, we need to open a terminal and navigate to the directory where the index.js file is located.

Then, we can run the following command:

“`

node index.js

“`

This will start the server and output the following message in the terminal:

“`

Server running at http://127.0.0.1:3000/

“`

Now, let’s open a web browser and navigate to http://127.0.0.1:3000/. This will display the “Hello World!” message on the page.

Conclusion:

Node.js is a powerful tool for building web applications on Linux. In this article, we discussed how to install and remove Node.js on Linux Mint using the apt package manager, and we also demonstrated how to create a simple web application using Node.js.

By following these steps, developers can begin building scalable, high-performance web applications using Node.js on Linux Mint. In conclusion, this article focused on the installation, removal, and usage of Node.js on Linux.

We discussed the necessary dependencies installation and how to install Node.js using the nvm package manager. We also learned how to remove Node.js using the apt package manager.

Lastly, we demonstrated how to create a simple web application using Node.js on Linux. Node.js, being an open-source, cross-platform server-side JavaScript runtime environment, provides developers with a powerful tool to build scalable, high-performance web applications.

The takeaway from this article is that by following these steps, developers can begin building their web applications with Node.js on Linux, opening up many possibilities to create efficient, robust, and secure web applications.

Popular Posts