###
> 前言
npm 安装n命令切换node版本 是全局切换 nvm切换node版本可以基于项目目录局部进行切换
> 下载nvm二进制包
######################################################################## ######################################################################## #下载nvm二进制包地址 https://github.com/nvm-sh/nvm/tags ######################################################################## ######################################################################## #安装nvm [root@test01 ~]# mkdir -p /home/wx/nvm-0.39 [root@test01 ~]# cd /home/wx/nvm-0.39 [root@test01 nvm-0.39]# wget https://github.com/nvm-sh/nvm/archive/refs/tags/v0.39.0.tar.gz [root@test01 nvm-0.39]# tar xf nvm-0.39.0.tar.gz [root@test01 nvm-0.39]# ls -l total 164 drwxrwxr-x 4 root root 4096 Oct 8 01:11 nvm-0.39.0 -rw-r--r-- 1 root root 163076 Nov 11 11:25 nvm-0.39.0.tar.gz
> 安装nvm
######################################################################## ######################################################################## [root@test01 nvm-0.39.0]# cd /home/wx/nvm-0.39/nvm-0.39.0 [root@test01 nvm-0.39.0]# ls -l total 252 -rw-rw-r-- 1 root root 2299 Oct 8 01:11 bash_completion -rw-rw-r-- 1 root root 5424 Oct 8 01:11 CODE_OF_CONDUCT.md -rw-rw-r-- 1 root root 1057 Oct 8 01:11 CONTRIBUTING.md -rw-rw-r-- 1 root root 3691 Oct 8 01:11 Dockerfile -rw-rw-r-- 1 root root 467 Oct 8 01:11 GOVERNANCE.md -rwxrwxr-x 1 root root 14984 Oct 8 01:11 install.sh -rw-rw-r-- 1 root root 1113 Oct 8 01:11 LICENSE.md -rw-rw-r-- 1 root root 5361 Oct 8 01:11 Makefile -rwxrwxr-x 1 root root 351 Oct 8 01:11 nvm-exec -rw-rw-r-- 1 root root 138067 Oct 8 01:11 nvm.sh -rw-rw-r-- 1 root root 2371 Oct 8 01:11 package.json -rw-rw-r-- 1 root root 2935 Oct 8 01:11 PROJECT_CHARTER.md -rw-rw-r-- 1 root root 40684 Oct 8 01:11 README.md -rwxrwxr-x 1 root root 1235 Oct 8 01:11 rename_test.sh -rw-rw-r-- 1 root root 882 Oct 8 01:11 ROADMAP.md drwxrwxr-x 9 root root 148 Oct 8 01:11 test -rwxrwxr-x 1 root root 2364 Oct 8 01:11 update_test_mocks.sh ######################################################################## ######################################################################## # 安装nvm [root@test01 nvm-0.39.0]# sh install.sh => Downloading nvm from git to '/root/.nvm' => Cloning into '/root/.nvm'... remote: Enumerating objects: 348, done. remote: Counting objects: 100% (348/348), done. remote: Compressing objects: 100% (297/297), done. remote: Total 348 (delta 39), reused 162 (delta 26), pack-reused 0 Receiving objects: 100% (348/348), 210.62 KiB | 0 bytes/s, done. Resolving deltas: 100% (39/39), done. * (detached from FETCH_HEAD) master => Compressing and cleaning up git repository => Appending nvm source string to /root/.bashrc => Appending bash_completion source string to /root/.bashrc => Close and reopen your terminal to start using nvm or run the following to use it now: export NVM_DIR="$HOME/.nvm" [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm [ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion ######################################################################## ######################################################################## [root@test01 nvm-0.39.0]# source /root/.bashrc ######################################################################## ######################################################################## [root@test01 nvm-0.39.0]# nvm -v 0.39.0
> Linux的root权限安装nvm后可用,切换用户以后报nvm: command not found解决办法
在root环境下安装nvm,一切正常,但是我切换成普通用户以后,就会报命令找不到,命令找不到肯定就是路径的问题。 以下就是解决办法: 1.首先root环境下,打开~/.bashrc文件,你会看到NVM_DIR,这就是nvm的安装路径。其中有个$HOME这个路径。然后我就去命令行打印了一下$HOME,结果如下:
之后我又切换了普通用户也查看了$HOME,查看结果如下:
两个$HOME都不一样,肯定引不到啊,而且/root下的文件只有root权限才可以访问。
2.将/root/.nvm文件夹移动到了/home/wx/nvm-0.39/(也可以移到别的文件夹下,该路径在环境变量中)下,然后修改~/.bashrc文件,将$HOME改成/home/wx/nvm-0.39
[root@test01 ~]# vim /root/.bashrc
.....
export NVM_DIR="/home/wx/nvm-0.39.0/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion
...
[wx@test01 ~]# vim /home/wx/.bashrc
...
export NVM_DIR="/home/wx/nvm-0.39.0/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion ... 3.重启~/.bashrc文件 [root@test01 ~]# source /root/.bashrc
[wx@test01 ~]# source /home/wx/.bashrc
4.查看nvm版本
[root@test01 ~]# nvm -v
0.39.0
[wx@test01 ~]# nvm -v
0.39.0
###