-
Notifications
You must be signed in to change notification settings - Fork 0
/
installNodejs.sh
64 lines (50 loc) · 1.9 KB
/
installNodejs.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
ver=""
if [ x$1 != x ] ;then
ver=$1
else
wget https://npm.taobao.org/mirrors/node/index.json -O /home/nodejsVersion.json
ver=$(cat /home/nodejsVersion.json | grep 'v[0-9]*\.[0-9]*\.[0-9]*' -o | awk NR==1)
fi
echo "download nodejs version $ver"
url="https://npm.taobao.org/mirrors/node/$ver/node-$ver-linux-x64.tar.xz"
wget $url -O /home/nodejs.tar.xz
recordfile=/home/nodejs.tar.xz
if [ ! -f "$recordfile" ] ;then
echo "download nodejs fail, please check your network or check nodejs version"
else
echo "remove old version of nodejs"
rm -rf /home/nodejs
echo "unzip nodejs package"
tar -xf /home/nodejs.tar.xz -C /home/
mv "/home/node-$ver-linux-x64" /home/nodejs
# 配置nodejs命令的环境变量, 全局命令的系统环境变量
echo "set nodejs system environment variables"
export PATH=$(echo $PATH | tr : "\n"| sort | uniq | tr "\n" :)
echo "export PATH=$PATH:/home/nodejs:/home/nodejs/bin:/home/node_modules:/home/node_modules/bin" > /etc/profile.d/nodejs.sh
echo "export NODE_PATH=/home/node_modules/lib/node_modules" >> /etc/profile.d/nodejs.sh
chmod 755 /etc/profile.d/nodejs.sh
source /etc/profile
# 配置npm全局命令路径
echo "set npm package global path"
npm config set prefix "/home/node_modules"
# 配置npm包缓存路径
echo "set npm package cache path"
npm config set cache "/home/npm_cache"
# 删除下载的文件
echo "remove cache install file"
rm /home/nodejsVersion.json /home/nodejs.tar*
# 打印输出
echo ""
echo ""
echo "nodejs install success"
echo ""
echo "nodejs path: /home/nodejs"
echo "npm global path: /home/node_modules"
echo "npm cache path: /home/npm_cache"
node=$(node -v)
npm=$(npm -v)
echo ""
echo "node --version: $node npm --version: $npm"
echo ""
echo "please exit and log in again if you are using it for the first time"
fi