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.

相关推荐
西门吹-禅3 分钟前
prisma
node.js
怪兽毕设1 小时前
基于SpringBoot的选课调查系统
java·vue.js·spring boot·后端·node.js·选课调查系统
晚风吹长发3 小时前
初步了解Linux中的线程概率及线程控制
linux·运维·服务器·开发语言·c++·centos·线程
i建模3 小时前
在 Ubuntu 中为 npm 切换国内镜像源
linux·ubuntu·npm
礼拜天没时间.3 小时前
《Docker实战入门与部署指南:从核心概念到网络与数据管理》:环境准备与Docker安装
运维·网络·docker·容器·centos
每天瞎忙的农民工3 小时前
Ubuntu 24 安装npm22
linux·运维·ubuntu·npm
心.c4 小时前
Vue3+Node.js实现文件上传分片上传和断点续传【详细教程】
前端·javascript·vue.js·算法·node.js·哈希算法
roamingcode4 小时前
我是如何 Vibe Coding,将 AI CLI 工具从 Node.js 迁移到 Rust 并成功发布的
人工智能·rust·node.js·github·claude·github copilot
Fleshy数模14 小时前
CentOS7 安装配置 MySQL5.7 完整教程(本地虚拟机学习版)
linux·mysql·centos
南宫码农18 小时前
神马影视8.5版本如意伪静态+视频教程
linux·运维·centos