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.

相关推荐
FungLeo4 小时前
成为全栈·Node 后端篇·容器化:给 Node 应用写一个像样的 Dockerfile
docker·node.js·dockerfile·成为全栈·后端服务容器·docker 容器
WeiXin_DZbishe8 小时前
基于springboot大学生提问箱系统-计算机毕设【课程设计】72593
javascript·vue.js·spring boot·vscode·python·node.js·php
xixiaoyunya11 小时前
Docker 容器化部署实战:从零搭建一套完整的 Nginx + Node.js + MySQL + Redis 项目环境
nginx·docker·node.js
晴天1611 小时前
npm install -f(--force)深度解析:作用原理、报错根源与风险避坑指南
前端·npm·node.js
还是大剑师兰特11 小时前
Swagger 在 Node.js 项目中的安装和完整使用方法
node.js
晴天1611 小时前
.npmrc 配置文件全解析:从换源到工程化的完整指南
npm·npmrc
FungLeo16 小时前
成为全栈·Node 后端篇·全文搜索:从 LIKE 到全文索引
node.js·全文索引·模糊搜索·全文搜索·成为全栈·like 搜索
刀鋒偏冷1 天前
CentOS 7 配置 Python 3.12.4 + RTX 4090 深度学习环境全记录
python·深度学习·centos
SatanII1 天前
华为云ECS实践:从创建、镜像制作到弹性伸缩完整实操指南
linux·运维·服务器·学习·centos·华为云
倾颜1 天前
AI Chat 长会话性能实践:消息虚拟化、动态高度与流式滚动设计
前端·react.js·node.js