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...');
})
相关推荐
happymagic34 分钟前
java spring boot做的jar包程序,如何实现自动运行启动
java·运维·服务器·spring boot·jar
星空你好37 分钟前
AI辅助运维:开源一个内网日志监控工具
运维
风曦Kisaki1 小时前
Kubernetes(K8s)笔记Day06: 持久化存储(emptyDir,hostPath,NFS),PV 和 PVC,StorageClass存储类
linux·运维·笔记·云原生·容器·kubernetes
Dr.kangder1 小时前
嵌入式处理器仿真技术——Hypervisor 原理与实践
服务器·嵌入式硬件·架构·嵌入式
_艾伦 耶格尔.1 小时前
基础I/O
linux
星蓝_starblue3 小时前
零服务器、零数据库!开源growth-board,利用GitHub自动管理刷题/学习/求职全流程
服务器·数据库·程序人生·系统架构·node.js·github·改行学it
fb_123454 小时前
Linux磁盘分区从入门到实操:MBR_GPT全解析+分区工具实战指南
java·linux·gpt
深念Y5 小时前
随身WiFi设备记录
linux·嵌入式·无线·射频·随身wifi·zte
柒号华仔5 小时前
【LINUX】Linux从闹钟到高精度定时器的实现
linux
Crazy________5 小时前
k8s部署若依微服务架构流程,v3.6.6
运维·云原生·容器·kubernetes·状态模式