linux安装nodejs

写在前面

因为工作需要,需要使用到nodejs,所以这里简单记录下学习过程。

1:安装

复制代码
wget https://nodejs.org/dist/v14.17.4/node-v14.17.4-linux-x64.tar.xz
tar xf node-v14.17.4-linux-x64.tar.xz
mkdir /usr/local/lib/node // 这一步骤根据具体的改就行,后面记得改成你本地的
mv node-v14.17.4-linux-x64 /usr/local/lib/node/nodejs

sudo vim /etc/profile
export NODEJS_HOME=/usr/local/lib/node/nodejs
export PATH=$NODEJS_HOME/bin:$PATH

source /etc/profile

最后查看:

复制代码
[root@localhost bin]# node -v
v14.17.4
[root@localhost bin]# npm -v
6.14.14

设置淘宝源,加速包下载:

复制代码
npm config set registry https://registry.npm.taobao.org/
npm install -g cnpm --registry=https://registry.npm.taobao.org
npm config get registry // 查看源

2:测试项目

复制代码
[root@localhost firstNodeJs]# npm init
This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sensible defaults.

See `npm help init` for definitive documentation on these fields
and exactly what they do.

Use `npm install <pkg>` afterwards to install a package and
save it as a dependency in the package.json file.

Press ^C at any time to quit.
package name: (firstnodejs) 
version: (1.0.0) 
description: 
entry point: (index.js) 
test command: 
git repository: 
keywords: 
author: 
license: (ISC) 
About to write to /root/nodejs/firstNodeJs/package.json:

{
  "name": "firstnodejs",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC"
}


Is this OK? (yes) yes
[root@localhost firstNodeJs]# ls
package.json
[root@localhost firstNodeJs]# ls
package.json
[root@localhost firstNodeJs]# vim hello.js
[root@localhost firstNodeJs]# cat hello.js 
console.log('hello from node');
[root@localhost firstNodeJs]# node hello.js 
hello from node

测试一个http的:

复制代码
[root@localhost firstNodeJs]# vim myhttp.js
[root@localhost firstNodeJs]# node myhttp.js 
server is running...

[root@localhost firstNodeJs]# cat myhttp.js 
const http=require('http');
const server=http.createServer();

server.on('request',function(request,response) {
	response.end("<h1 style='color:red;'>hellooooooooo!!!</h1>");		
})
server.listen(3001,function() {
	console.info('server is running...');
})
相关推荐
GoWjw6 分钟前
Linux虚拟文件系统(1)
运维·服务器·数据库
SunTecTec33 分钟前
Linux grep -r 查找依赖包是否存在依赖类 Class
linux·运维·服务器
Johny_Zhao1 小时前
AI+自动化测试系统方案:网络设备与网络应用智能测试
linux·网络·人工智能·python·网络安全·docker·ai·信息安全·云计算·ansible·shell·cisco·huawei·系统运维·itsm·华三·deepseek
Javis2111 小时前
【Linux高级全栈开发】2.1.2 事件驱动reactor的原理与实现
linux·运维·php
FBI HackerHarry浩1 小时前
Linux云计算训练营笔记day10(MySQL数据库)
linux·运维·数据库·笔记·mysql
南暮思鸢1 小时前
vulnhub靶场——secarmy
linux·网络安全·vulnhub靶场·write up·secarmy
掘金-我是哪吒2 小时前
分布式微服务系统架构第134集:笔记1运维服务器经验,高并发,大数据量系统
运维·笔记·分布式·微服务·系统架构
YOYO--小天2 小时前
RK3588 ADB使用
linux·嵌入式硬件·adb
猴子请来的逗比4892 小时前
tomcat查看状态页及调优信息
服务器·学习·tomcat·firefox
Strugglingler2 小时前
Shell 脚本
linux·bash·shell