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...');
})
相关推荐
风语者日志16 分钟前
[LitCTF 2023]Vim yyds
linux·编辑器·vim
Thexhy34 分钟前
在centos 7上配置FIP服务器的详细教程!!!
linux·运维·centos
chao18984439 分钟前
C 文件操作全解速览
服务器·c语言·c#
FJW0208141 小时前
DevOps——CI/CD持续集成与持续交付/部署的理解与部署
运维·ci/cd·devops
Java 码农2 小时前
Linux shell sed 命令基础
linux·运维·服务器
yong15858553432 小时前
1. Linux C++ muduo 库学习——库的编译安装
linux·c++·学习
fyakm2 小时前
Linux网络接口配置:静态IP与动态IP设置(附代码示例)
linux·运维·tcp/ip
怀旧,2 小时前
【Linux系统编程】5. 基础开发⼯具(下)
linux·运维·服务器
喵叔哟2 小时前
10. 从0到上线:.NET 8 + ML.NET LTR 智能类目匹配实战--Web API 接口与前端集成:部署与生产运维:稳定性、可观测与成本
运维
杰克崔2 小时前
内核里常用宏BUG_ON/WARN_ON/WARN_ONCE
linux·运维·服务器