230429_nvm尝试node版本管理

Haoliang Tang Lv2

这周在github上试了好几个vue的mail项目,下载后,npm install,npm run dev/serve就各种版本不兼容的报错。本来想要在pacman装旧版本的nodejs,问了lph,然后可以用nvm相当于一个虚拟环境って教えてもらった。

类比pycharm新建项目时可以选虚拟环境,就不会用本地的解释器

Manjaro上安装nvm

pacman仓库里就有nvm

1
sudo pacman -S nvm

此时新开终端,敲nvmnvm -v都是没输出的。需要配置shell(官网github也是)

以下是pacman安装后输出的提示信息:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
You need to source nvm before you can use it. Do one of the following
or similar depending on your shell (and then restart your shell):

echo 'source /usr/share/nvm/init-nvm.sh' >> ~/.bashrc
echo 'source /usr/share/nvm/init-nvm.sh' >> ~/.zshrc

You can now install node.js versions (e.g. nvm install 10) and
activate them (e.g. nvm use 10).

init-nvm.sh is a convenience script which does the following:

[ -z "$NVM_DIR" ] && export NVM_DIR="$HOME/.nvm"
source /usr/share/nvm/nvm.sh
source /usr/share/nvm/bash_completion
source /usr/share/nvm/install-nvm-exec

You may wish to customize and put these lines directly in your
.bashrc (or similar) if, for example, you would like an NVM_DIR
other than ~/.nvm or you don't want bash completion.

See the nvm readme for more information: https://github.com/creationix/nvm

那么zsh就终端执行

1
$ echo 'source /usr/share/nvm/init-nvm.sh' >> ~/.zshrc

fish的话并不完全支持bash语法,在 ~/.config/fish/config.fish里直接写入source /usr/share/nvm/init-nvm.sh是不work的。

去看nvm项目的Issues https://github.com/nvm-sh/nvm/issues/303#issuecomment-121086278

需要使用fish的package manager(如oh-my-fish, fisher)装bass。Bass makes it easy to use utilities written for Bash in a fish shell.

  • 下载Bass: omf install bass/fisher install edc/bass
  • 编辑~/.config/fish/config.fish:
1
2
3
function nvm
bass source /usr/share/nvm/init-nvm.sh ';' nvm $argv
end

==fish的话,其实不如用这个fish插件== https://github.com/jorgebucaran/nvm.fish

还可以用node use system切换回系统的nodejs

然后新开终端,敲nvmnvm -v都有输出了

nvm使用

目前的本机环境node v19.8.1, npm 8.19.2。跑一个vue-element-admin 项目 https://github.com/PanJiaChen/vue-element-admin

1
2
3
4
5
6
7
8
9
10
11
# clone the project
git clone https://github.com/PanJiaChen/vue-element-admin.git

# enter the project directory
cd vue-element-admin

# install dependency
npm install

# develop
npm run dev

This will automatically open http://localhost:9527

npm run dev时报错:error:0308010C:digital envelope routines::unsupported

网上查了下,就是node版本高了。然后就可以用nvm来切换版本


If you want to see what versions are available to install, You can list available versions using ls-remote:

1
nvm ls-remote

If you want to see what versions are installed:

1
nvm ls

use之前先要install

1
nvm install 16

此时我在当前目录下node -v版本是v16.20.0。但其他目录下终端也变成v16.20.0了。

由于当前目录下已经是用老版本的nodejs了,直接npm run dev

OK,它跑起来了!

参考资料

https://github.com/nvm-sh/nvm 官方

NVM(Node Version Manager)を使ってNode.jsの開発環境を作る

What is NPM, and why do we need it? | Tutorial for beginners 讲得很好的npm教程

そもそもnpmからわからない 主要是讲npm的,但先装了nvm

  • Title: 230429_nvm尝试node版本管理
  • Author: Haoliang Tang
  • Created at : 2023-04-29 00:00:00
  • Updated at : 2023-11-02 20:11:29
  • Link: https://hl-tang.github.io/2023/04/29/230429_nvm尝试node版本管理,虚拟环境跑github别人写的repo/
  • License: This work is licensed under CC BY-NC-SA 4.0.
 Comments
On this page
230429_nvm尝试node版本管理