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...');
})
相关推荐
likfishdn19 分钟前
Linux的文件与目录管理
linux·运维·服务器
渲染101专业云渲染21 分钟前
川翔云电脑优势总结
服务器·3d·电脑·blender·maya
chian-ocean39 分钟前
Linux 文件缓冲区:高效数据访问的幕后推手
linux·运维·服务器
加油=^_^=40 分钟前
【Linux】进程优先级 | 进程调度(三)
linux·运维·服务器
橘子味的茶二1 小时前
高级系统架构师--第十章:计算机网络
服务器·计算机网络·系统架构
吃汤圆的抹香鲸1 小时前
GoLand 安装包 绿色版 Win,Mac,Linux 包含IntelliJ全家桶 专为Go语言设计的集成开发环境(IDE)
linux·windows·macos·go·intellij-idea·go1.19
紫菜(Nori)1 小时前
短连接服务器压测-wrk
服务器·压力测试
追寻光1 小时前
Cannot find a valid baseurl for repo: base/7/x86_64
linux
元气满满的热码式1 小时前
Docker数据卷操作实战
linux·运维·docker·云原生·容器
陆沙2 小时前
deepseek-r1-centos-本地服务器配置方法
linux·服务器·centos