关于基础的nvm, nrm控制node版本和npm源地址的使用

818 阅读4分钟

在刚刚学习vite的时候,我发现下载vite构建需要nodejs 14.16以上的版本. 但是我的电脑上有兼顾node低版本的项目,盲目删除现有node版本可能会导致出现未知问题,经过一段时间的了解我才知道有nvm工具

nvm

全称: Node Version Manager 用于node版本的控制

使用

首先现在nvm官方博客github下下载对应的安装程序 地址: 'github.com/coreybutler…'

滑动到下面可见安装包

QQ截图20220922154108.png

这里有 .,exe安装程序, 以及zip压缩文件

选择安装程序安装(也可解压安装包安装)

安装过程基本一路next, 但是注意跳出的需要指定的安装路径一定不要是中文或者包含空格、@字符, 否则后续可能出现路径问题。

安装过程中如果本地有node的话,可以允许安装应用识别本地的node

安装完成后使用

   nvm

来查看是否安装成功

QQ截图20220922154842.png

这里显示安装成功

检查本地可用node版本

nvm list

QQ截图20220922155046.png

这里我本地有一个版本(*表示当前我使用的nodejs版本)

假如现在需要安装 nodejs 14.18.0版本

可使用nvm list available 查看当前所有nodejs 可用上线版本


PS C:\Users\86132> nvm list available

|   CURRENT    |     LTS      |  OLD STABLE  | OLD UNSTABLE |
|--------------|--------------|--------------|--------------|
|    18.9.0    |   16.17.0    |   0.12.18    |   0.11.16    |
|    18.8.0    |   16.16.0    |   0.12.17    |   0.11.15    |
|    18.7.0    |   16.15.1    |   0.12.16    |   0.11.14    |
|    18.6.0    |   16.15.0    |   0.12.15    |   0.11.13    |
|    18.5.0    |   16.14.2    |   0.12.14    |   0.11.12    |
|    18.4.0    |   16.14.1    |   0.12.13    |   0.11.11    |
|    18.3.0    |   16.14.0    |   0.12.12    |   0.11.10    |
|    18.2.0    |   16.13.2    |   0.12.11    |    0.11.9    |
|    18.1.0    |   16.13.1    |   0.12.10    |    0.11.8    |
|    18.0.0    |   16.13.0    |    0.12.9    |    0.11.7    |
|    17.9.1    |   14.20.0    |    0.12.8    |    0.11.6    |
|    17.9.0    |   14.19.3    |    0.12.7    |    0.11.5    |
|    17.8.0    |   14.19.2    |    0.12.6    |    0.11.4    |
|    17.7.2    |   14.19.1    |    0.12.5    |    0.11.3    |
|    17.7.1    |   14.19.0    |    0.12.4    |    0.11.2    |
|    17.7.0    |   14.18.3    |    0.12.3    |    0.11.1    |
|    17.6.0    |   14.18.2    |    0.12.2    |    0.11.0    |
|    17.5.0    |   14.18.1    |    0.12.1    |    0.9.12    |
|    17.4.0    |   14.18.0    |    0.12.0    |    0.9.11    |
|    17.3.1    |   14.17.6    |   0.10.48    |    0.9.10    |

选择下载对应版本

使用

nvm install 版本号

即可

QQ截图20220922155707.png

下载完成

PS C:\Users\86132> nvm install 14.18.0
Downloading node.js version 14.18.0 (64-bit)...
Complete
Creating C:\Users\86132\AppData\Roaming\nvm\temp

Downloading npm version 6.14.15... Complete
Installing npm v6.14.15...

Installation complete. If you want to use this version, type

nvm use 14.18.0

检查本地node

PS C:\Users\86132> nvm list

  * 16.17.0 (Currently using 64-bit executable)
    14.18.0

使用下载好的版本

nvm use 版本号

QQ截图20220922160225.png

在使用这条命令的时候我的命令行界面提示乱码,查询得知是权限不够,所以这里使用管理原打开命令行,

再使用命令就没问题了

注意: 我的乱码解析后是提示权限不同,但是实际上可能因为其他原因出现不同的乱码(路径问题等等)

总之 ,当前我的node版本顺利切换到 14.18.0了。

PS C:\Users\86132> nvm list

    16.17.0
  * 14.18.0 (Currently using 64-bit executable)

补充: nvm 可以删除对应的node版本 --- nvm uninstall 版本号 另外下载时也可指定下载的安装包的 window系统位数

使用nrm 管理npm 镜像源

nrm 全称 npm registry manager

因为npm 默认源在国外的原因,导致我们在使用的时候下载依赖会特别的慢, 虽然可以设置镜像源,但是切换的过程还是麻烦,这里可以使用nrm统一管理

下载nrm

npm install nrm -g

直接在命令控制台输入 nrm 可以看到有命令解析的话就说明安装成功了

PS C:\Users\86132> nrm
Usage: cli [options] [command]

Options:
  -V, --version                           output the version number
  -h, --help                              output usage information

Commands:
  ls                                      List all the registries
  current [options]                       Show current registry name or URL
  use <registry>                          Change registry to registry
  add <registry> <url> [home]             Add one custom registry
  login [options] <registryName> [value]  Set authorize information for a custom registry with a base64 encoded string or username and pasword
  set-hosted-repo <registry> <value>      Set hosted npm repository for a custom registry to publish packages
  set-scope <scopeName> <value>           Associating a scope with a registry
  del-scope <scopeName>                   Remove a scope
  set [options] <registryName>            Set custom registry attribute
  rename <registryName> <newName>         Set custom registry name
  del <registry>                          Delete one custom registry
  home <registry> [browser]               Open the homepage of registry with optional browser
  publish [options] [<tarball>|<folder>]  Publish package to current registry if current registry is a custom registry.
   if you're not using custom registry, this command will run npm publish directly
  test [registry]                         Show response time for specific or all registries
  help                                    Print this help
   if you want to clear the NRM configuration when uninstall you can execute "npm uninstall nrm -g -C or npm uninstall nrm -g --clean"

查看可用源

使用 nrm ls

PS C:\Users\86132> nrm ls

  npm ---------- https://registry.npmjs.org/
  yarn --------- https://registry.yarnpkg.com/
  tencent ------ https://mirrors.cloud.tencent.com/npm/
  cnpm --------- https://r.cnpmjs.org/
  taobao ------- https://registry.npmmirror.com/
  npmMirror ---- https://skimdb.npmjs.com/registry/

可以看到有多个源

使用源

nrm use 源名称

这里使用淘宝镜像

PS C:\Users\86132> nrm use taobao


   Registry has been set to: https://registry.npmmirror.com/

使用成功

使用自定义源

nrm add 自定义源名称 源地址

nrm add registry http://registry.npm.frp.trmap.cn/

删除源

nrm uninstall url

测试速度

nrm test 源名称

PS C:\Users\86132> nrm test cnpm

  cnpm ----- 437ms

npx

这里提一下npx是什么

用于执行依赖包中的二进制文件

npx 在npm中已经是内置了


项目中下载一个依赖包时(如webpack) ,直接终端运行wenpack命令是不可行的, 需要到package.json中配置命令才行 npm run webpack

而npx只需 npx webpack

这就很简单了