Home >Operation and Maintenance >Linux Operation and Maintenance >How to install the latest Nodejs on CentOS and RHEL

How to install the latest Nodejs on CentOS and RHEL

不言
不言Original
2019-03-29 15:51:232424browse

Node.js is a platform based on the Chrome JavaScript runtime that makes it easy to build fast, scalable web applications. The latest version of node.js yum repository is maintained by its official website. Use this article to add yum repository and install latest Nodejs to CentOS/RHEL 7/6 system using simple commands.

How to install the latest Nodejs on CentOS and RHEL

To install a specific nodejs version, you can refer to the article: Use NVM to install a specific Nodejs version.

Step 1: Add node.js yum repository

First, you need to enable node.js yum storage in the system provided by the Node.js official website libraries, development tools are also needed to build native add-ons to be installed on the system.

Latest version:

# yum install -y gcc-c++ make
# curl -sL https://rpm.nodesource.com/setup_11.x | sudo -E bash -

Stable release:

# yum install -y gcc-c++ make
# curl -sL https://rpm.nodesource.com/setup_10.x | sudo -E bash -

Step 2: Install node.js on CentOS

In the system After adding the yum repository, you can install the node.js package. NPM will also be installed with node.js. This command will also install many other dependent packages on the system.

# sudo yum install nodejs

Recommended: yarn installation (node ​​module manager)

Step 3: Check node.js and npm versions

After installing node.js, you need to verify and check the installed version. More details about the current version can be found on the official node.js website.

# node -v 
v11.12.0

Also, check the version of NPM.

# npm -v 
6.7.0

Step 4: Create a demo web server (optional)

This is an optional step. If you want to test node.js installation. Let’s create a web server with the text “Welcome Node.js”. Create a demo_server.js file

# vim demo_server.js

and add the following content

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

Now start the web server using the command.

# node --debug demo_server.js

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

The web server has been started on port 3001. Now visit http://127.0.0.1:3001/url in your browser.

This article has ended here. For more other exciting content, you can pay attention to the Linux Video Tutorial column on the PHP Chinese website!

The above is the detailed content of How to install the latest Nodejs on CentOS and RHEL. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn