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.

相关推荐
严肃小白兔16 小时前
TanStack Router 更新构建工具链:Node.js 兼容性、路由分割与前端工程实践
前端·node.js·状态模式
一缕82年的清风1 天前
用了 3 年 Notion 和 Obsidian 后,为什么我最终把所有身家搬到了思源笔记?
node.js
Dumbness2 天前
一份 JSON 生成增删改查接口,入参清洗漏了什么:Number(" ") 是 0,搜 100% 把 1000 元券也搜出来
node.js·express
FungLeo3 天前
成为全栈·基础补充·Node.js 运行时:事件循环与异步
javascript·node.js·事件循环·后端开发·异步编程·成为全栈
用户087308654873 天前
用 GUI 自动化写了一个群聊 AI:5 个真实事故,和 7 条让 AI 不像机器人的设计
node.js
David猪大卫4 天前
【Linux修炼】环境变量、进程地址空间
linux·c语言·经验分享·笔记·学习·ubuntu·centos
Dumbness5 天前
毕设后台导出 CSV 的 4 个坑:date 字段漏了公式防护,搜索框输入 % 能扫全表
node.js·express
Dragon~Snow6 天前
Centos Stream10 安装 MySQL-9.7.2-linux-glibc2.28-x86_64.tar
linux·mysql·centos
温暖小土6 天前
在 CentOS 上安装 Redis Stack
linux·redis·centos