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.

相关推荐
多厘11 分钟前
使用 nvm 管理多版本 Node 项目依赖
node.js
TT_Close19 小时前
🐟 发布中心进度同步:8 个商店的上传功能开发完毕,正抓紧测试
flutter·npm·visual studio code
多厘19 小时前
find-skills: 使用 skills 快速找到最匹配 skills
npm
穷人小水滴1 天前
编译 LLVM: 跨平台 npm 二进制包
npm·编译器·llvm
前端双越老师1 天前
Skills 是什么?如何用于 Agent 开发?
人工智能·node.js·agent
San302 天前
AI 时代的“USB-C”接口:MCP 核心原理与实战
langchain·node.js·mcp
helloweilei3 天前
javascript 结构化克隆
javascript·node.js
小蜜蜂dry4 天前
nestjs学习 - 控制器、提供者、模块
前端·node.js·nestjs
San304 天前
手写 Mini Cursor:基于 Node.js 与 LangChain 的开发实战
langchain·node.js·agent
前端付豪4 天前
Nest 项目小实践之图书增删改查
前端·node.js·nestjs