How to install Node.js and NPM on CentOS

How to install Node.js and NPM on CentOS

Download Node.js

菜鸟教程-Node.js 安装配置

Introduction

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 explain the steps of installing node.js and npm in CentOS.

Step 1: Add node.js yum repository

First we need to add yum repository of node.js to our system which is sourced from nodejs' official website. Run the following commands in succession to add the yum repository.

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

如果报错如下:

Error: Failed to download metadata for repo 'nodesource': Cannot download repomd.xml: Cannot download repodata/repomd.xml: All mirrors were tried

尝试执行如下命令解决

sudo rm -r /var/cache/dnf

依然不行,去官网查看新版本的重新尝试。

Welcome to Node.js RPM repository

Step 2: Install node.js and NPM

Now it's time to install the node.js package and NPM package. Run the following command to do so. This single command will install node.js, npm and also other dependent packages.

yum install nodejs
Copy code

Step 3: Verify versions

Having installed the packages, you need to check their versions.

For checking node.js version:

node -v

#output
v6.9.4
Copy code

For checking npm version:

npm -v

#output
3.10.10
Copy code

Step 4: Testing the installation

You can test your installation by creating a test file, say test_server.js

vim test_server.js
Copy code

Then add the following content in the file.

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

Now start the web server using the below command

node --debug test_server.js

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

Web server has started which can be accessed using URL http://127.0.0.1:3001/ in your browser.

相关推荐
蛊明1 小时前
下载CentOS 10
linux·运维·centos
m0_748256142 小时前
最新最详细的配置Node.js环境教程
node.js
m0_748250932 小时前
node.js下载、安装、设置国内镜像源(永久)(Windows11)
node.js
茂茂在长安3 小时前
Linux 命令大全完整版(11)
java·linux·运维·服务器·前端·centos
心随_风动4 小时前
CentOS 下安装和配置 HTTPD 服务的详细指南
linux·运维·centos
信阳农夫4 小时前
centos 7只能安装到3.6.8
linux·运维·centos
OpenTiny社区6 小时前
Node.js技术原理分析系列——Node.js的perf_hooks模块作用和用法
前端·node.js
m0_748235077 小时前
如何自由切换 Node.js 版本?
node.js
HugeYLH8 小时前
解决npm问题:错误的代理设置
前端·npm·node.js
Anna_Tong8 小时前
阿里云如何协助解决操作系统兼容性问题
linux·服务器·ubuntu·阿里云·centos·云计算·系统迁移