Linux 安装nodejs

Linux 安装nodejs

1.下载

wget https://npm.taobao.org/mirrors/node/v8.9.3/node-v8.9.3-linux-x64.tar.xz

2.将其放入opt文件夹下面,可以理解为D:/Software

3.解压

tar -vxf node-v8.9.3-linux-x64.tar.xz

4.将解压包移动到/usr/local

mv node-v8.9.3-linux-x64/ /usr/local/node

5.配置全局变量

vim /etc/profile

增加一行

export PATH=$PATH:/usr/local/node/bin

重新加载变量

source /etc/profile

6.测试是否成功

[root@local bin]# node -v
v8.9.3
[root@local bin]# npm -v
5.5.1

7.测试

新建hello.js


var http = require("http");
http.createServer(function(request, response) {
    response.writeHead(200, {
        "Content-Type" : "text/plain" // 输出类型
    });
    response.write("Hello World!");// 页面输出
    response.end();
}).listen(8100); // 监听端口号
console.log("nodejs start listen 8100 port!");
                                                 
[root@local nodeSpace]# node hello.js 
nodejs start listen 8100 port!
[root@local nodeSpace]# netstat -anpo | grep :8100
tcp6       0      0 :::8100                 :::*                    LISTEN      5556/node            off (0.00/0/0)
[root@local nodeSpace]# ps -ef | grep node
root       5556   4418  0 09:51 pts/1    00:00:00 node hello.js
root       5567   5105  0 09:52 pts/2    00:00:00 grep --color=auto node

原文地址:https://www.cnblogs.com/jiqing9006/p/8383629.html