首页·终端命令

nvm use

包与运行时

用法

nvm use <version> | nvm use --lts

切换当前 shell 使用的已装 Node 版本;改动只作用于这个 shell,新开的终端或 CI 会回到默认值,而 nvm 是个 shell 函数,脚本或 Makefile 里得先 source nvm.sh。

常用选项

选项含义
--ltsthe newest long-term-support line you have installed
nvm install <version>download it first; use only switches to installed ones
nvm alias default <version>what a brand-new shell should start with
nvm lsthe versions on this machine
nvm currentwhich one is active in this shell

示例

nvm use 22

switches this shell to node 22

nvm use --lts

switches to the newest installed LTS

nvm alias default 22

so new terminals and editors also get 22

问题永远是什么装到哪里:装进这个项目,还是装到整台机器上。

怎么看

  • 方括号 [ ] 表示这部分可以不写。
  • 省略号 … 表示可以写多个。
  • 选项区分大小写——有些命令里 -r 和 -R 不是一回事。

常见问题

Q. nvm use 是做什么的?

切换当前 shell 使用的已装 Node 版本;改动只作用于这个 shell,新开的终端或 CI 会回到默认值,而 nvm 是个 shell 函数,脚本或 Makefile 里得先 source nvm.sh。

Q. 怎么写?

nvm use <version> | nvm use --lts —— 方括号表示可以省略的部分。

Q. 值得记的选项有几个?

这里列了 5 个,完整列表在 man nvm。这条命令属于包与运行时。

相关命令

man 手册: man nvm-use