nodejs国内镜像及切换版本工具nvm

使用nvm工具安装nodejs版本的流程是:

1、安装nvm(建议setup版,noinstall没用明白)

2、通过nvm install 14.14.0安装nodejs

3、执行nvm use 14.14.0 使nodejs生效

ps:

nvm list 查询已安装的版本

nvm list available 查询全网可用的版本

在nvm安装的nodejs中,安装yarn,并切换国内镜像

1、安装yarn:   npm install -g yarn

2、一次性解除windows命令行对脚本执行的限制:  Set-ExecutionPolicy Unrestricted

3、设置yarn源为国内淘宝源:  yarn config set registry https://registry.npmmirror.com

ps:

yarn -v  // 查看yarn 版本
yarn config list  // 查看yarn配置
yarn config get registry   // 查看当前yarn源

注意:这里不使用npm,似乎不用切换npm源为国内淘宝源,但是建议也切换到淘宝源,以免两个源不统一,切换命令与yarn类似:

npm config set registry https://registry.npmmirror.com

操作全过程:

Windows PowerShell
版权所有 (C) Microsoft Corporation。保留所有权利。

尝试新的跨平台 PowerShell https://aka.ms/pscore6

PS C:\Windows\system32> nvm -v
1.1.12
PS C:\Windows\system32> nvm list

No installations recognized.
PS C:\Windows\system32> nvm list available

|   CURRENT    |     LTS      |  OLD STABLE  | OLD UNSTABLE |
|--------------|--------------|--------------|--------------|
|    21.5.0    |   20.11.0    |   0.12.18    |   0.11.16    |
|    21.4.0    |   20.10.0    |   0.12.17    |   0.11.15    |
|    21.3.0    |    20.9.0    |   0.12.16    |   0.11.14    |
|    21.2.0    |   18.19.0    |   0.12.15    |   0.11.13    |
|    21.1.0    |   18.18.2    |   0.12.14    |   0.11.12    |
|    21.0.0    |   18.18.1    |   0.12.13    |   0.11.11    |
|    20.8.1    |   18.18.0    |   0.12.12    |   0.11.10    |
|    20.8.0    |   18.17.1    |   0.12.11    |    0.11.9    |
|    20.7.0    |   18.17.0    |   0.12.10    |    0.11.8    |
|    20.6.1    |   18.16.1    |    0.12.9    |    0.11.7    |
|    20.6.0    |   18.16.0    |    0.12.8    |    0.11.6    |
|    20.5.1    |   18.15.0    |    0.12.7    |    0.11.5    |
|    20.5.0    |   18.14.2    |    0.12.6    |    0.11.4    |
|    20.4.0    |   18.14.1    |    0.12.5    |    0.11.3    |
|    20.3.1    |   18.14.0    |    0.12.4    |    0.11.2    |
|    20.3.0    |   18.13.0    |    0.12.3    |    0.11.1    |
|    20.2.0    |   18.12.1    |    0.12.2    |    0.11.0    |
|    20.1.0    |   18.12.0    |    0.12.1    |    0.9.12    |
|    20.0.0    |   16.20.2    |    0.12.0    |    0.9.11    |
|    19.9.0    |   16.20.1    |   0.10.48    |    0.9.10    |

This is a partial list. For a complete list, visit https://nodejs.org/en/download/releases
PS C:\Windows\system32> nvm install 14.17
Downloading node.js version 14.17.6 (64-bit)...
Complete
Creating C:\env\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.17.6
PS C:\Windows\system32>
PS C:\Windows\system32>
PS C:\Windows\system32> nvm list

    14.17.6
PS C:\Windows\system32> nvm use 14
Now using node v14.17.6 (64-bit)
PS C:\Windows\system32> nvm list

  * 14.17.6 (Currently using 64-bit executable)
PS C:\Windows\system32> node -v
v14.17.6
PS C:\Windows\system32> npm -v
6.14.15
PS C:\Windows\system32> npm install -g yarn

> yarn@1.22.21 preinstall C:\env\nodejs\node_modules\yarn
> :; (node ./preinstall.js > /dev/null 2>&1 || true)

C:\env\nodejs\yarn -> C:\env\nodejs\node_modules\yarn\bin\yarn.js
C:\env\nodejs\yarnpkg -> C:\env\nodejs\node_modules\yarn\bin\yarn.js
+ yarn@1.22.21
added 1 package in 2.784s
PS C:\Windows\system32> npm config ls
; cli configs
metrics-registry = "https://registry.npmjs.org/"
scope = ""
user-agent = "npm/6.14.15 node/v14.17.6 win32 x64"

; node bin location = C:\env\nodejs\node.exe
; cwd = C:\Windows\system32
; HOME = C:\Users\admin
; "npm config ls -l" to show all defaults.

PS C:\Windows\system32> yarn config list
yarn : 无法加载文件 C:\env\nodejs\yarn.ps1,因为在此系统上禁止运行脚本。有关详细信息,请参阅 https:/go.microsoft.com/fw
link/?LinkID=135170 中的 about_Execution_Policies。
所在位置 行:1 字符: 1
+ yarn config list
+ ~~~~
    + CategoryInfo          : SecurityError: (:) [],PSSecurityException
    + FullyQualifiedErrorId : UnauthorizedAccess
PS C:\Windows\system32> Set-ExecutionPolicy Unrestricted

执行策略更改
执行策略可帮助你防止执行不信任的脚本。更改执行策略可能会产生安全风险,如 https:/go.microsoft.com/fwlink/?LinkID=135170
中的 about_Execution_Policies 帮助主题所述。是否要更改执行策略?
[Y] 是(Y)  [A] 全是(A)  [N] 否(N)  [L] 全否(L)  [S] 暂停(S)  [?] 帮助 (默认值为“N”): y
PS C:\Windows\system32> yarn config list
yarn config v1.22.21
info yarn config
{
  'version-tag-prefix': 'v',
  'version-git-tag': true,
  'version-commit-hooks': true,
  'version-git-sign': false,
  'version-git-message': 'v%s',
  'init-version': '1.0.0',
  'init-license': 'MIT',
  'save-prefix': '^',
  'bin-links': true,
  'ignore-scripts': false,
  'ignore-optional': false,
  registry: 'https://registry.yarnpkg.com',
  'strict-ssl': true,
  'user-agent': 'yarn/1.22.21 npm/? node/v14.17.6 win32 x64'
}
info npm config
{}
Done in 0.10s.
PS C:\Windows\system32> yarn config get registry
https://registry.yarnpkg.com
PS C:\Windows\system32> yarn config set registry https://registry.npmmirror.com
yarn config v1.22.21
success Set "registry" to "https://registry.npmmirror.com".
Done in 0.09s.
PS C:\Windows\system32> yarn config get registry
https://registry.npmmirror.com
PS C:\Windows\system32>

淘宝 NPM 镜像站(http://npm.taobao.org)已更换域名,新域名:

  • Web 站点:https://npmmirror.com

  • Registry Endpoint:https://registry.npmmirror.com

详见:

【望周知】淘宝 NPM 镜像换域名了!

为了方便切换nodejs版本,可以使用nvm工具,安装方法参考:

window下安装并使用nvm(含卸载node、卸载nvm、全局安装npm)_window安装nvm-CSDN博客

nvm新淘宝镜像配置settings.txt:

root: D:\env\nvm
path: D:\env\nodejs

node_mirror: https://npmmirror.com/mirrors/node/
npm_mirror: https://npmmirror.com/mirrors/npm/

注意下载nvm是不要下载nvm-update.zip,nvm-update.exe运行时没有安装界面。

nvm安装包下载地址:

https://github.com/coreybutler/nvm-windows/releases

后面在尝试统一npm,但不实用,有兴趣看看,不建议也不需要这样配。

为不同版本的nodejs安装配置统一的npm:(但是nodejs版本需要支持npm版本,当nodejs是v14.17.6时,就不支持npm版本v10.2.2,所以有时候统一的npm不好用)

PS C:\Users\admin> npm config set prefix "D:\env\nvm\npm"
PS C:\Users\admin> npm install npm -g --registry=https://registry.npmmirror.com
D:\env\nvm\npm\npm -> D:\env\nvm\npm\node_modules\npm\bin\npm-cli.js
D:\env\nvm\npm\npx -> D:\env\nvm\npm\node_modules\npm\bin\npx-cli.js
npm WARN notsup Unsupported engine for npm@10.2.2: wanted: {"node":"^18.17.0 || >=20.5.0"} (current: {"node":"14.17.6","npm":"6.14.15"})
npm WARN notsup Not compatible with your version of node/npm: npm@10.2.2

+ npm@10.2.2
added 227 packages from 112 contributors in 22.403s

注意:还需要在用户变量中添加 NPM_HOME = D:\env\nvm\npm,path中添加%NPM_HOME%。这里需要注意的是,%NPM_HOME%要添加在%NVM_SYMLINK%之前,避免npm访问到的是nodejs中自带的npm包管理工具。

重新指定npm路径(prefix):

npm config set prefix "D:\env\nvm\npm6_14_15"

指定prefix后安装yarn

npm install -g yarn

安装yarn后查询npm list 仅看到yarn等很少的库,表明这时npm全局环境是比较干净的。

PS C:\Users\admin> npm config ls
; cli configs
metrics-registry = "https://registry.npmmirror.com/"
scope = ""
user-agent = "npm/6.14.15 node/v14.17.6 win32 x64"

; userconfig C:\Users\admin\.npmrc
prefix = "D:\\env\\nvm\\npm6_14_15"
registry = "https://registry.npmmirror.com/"

; node bin location = D:\env\nodejs\node.exe
; cwd = C:\Users\admin
; HOME = C:\Users\admin
; "npm config ls -l" to show all defaults.

PS C:\Users\admin> npm list
npm WARN read-shrinkwrap This version of npm is compatible with lockfileVersion@1, but package-lock.json was generated for lockfileVersion@3. I'll try to do my best with it!
C:\Users\admin
+-- nrm@1.2.6
| +-- chalk@4.1.2
| | +-- ansi-styles@4.3.0
| | | `-- color-convert@2.0.1
| | |   `-- color-name@1.1.4
| | `-- supports-color@7.2.0
| |   `-- has-flag@4.0.0
| +-- commander@8.3.0
| +-- ini@2.0.0
| +-- node-fetch@2.6.6
| | `-- whatwg-url@5.0.0
| |   +-- tr46@0.0.3
| |   `-- webidl-conversions@3.0.1
| `-- open@8.4.2
|   +-- define-lazy-prop@2.0.0
|   +-- is-docker@2.2.1
|   `-- is-wsl@2.2.0
|     `-- is-docker@2.2.1 deduped
`-- yarn@1.22.19

 接下来设置yarn命令到系统环境变量path中,使yarn命令可以在命令行的任意路径下执行。

  • 复制bin地址:D:\nodejs\node_global\node_modules\yarn\bin
    在这里插入图片描述在这里插入图片描述
  • 重要!添加D:\nodejs\node_global\node_modules\yarn\bin系统变量-path 中,是为了 在windows-cmd中能启动 npm.cmd 文件!!
    在这里插入图片描述
  • 重要! 记得重新打开dos窗口(不管修改什么程序的环境变量,都必须重新打开dos窗口,才会生效)

ps :我的D:\env\nvm\npm6_14_15\node_modules\yarn\bin里为啥没有npm.cmd呢?

 重启命令行窗口后,设置yarn国内源镜像

yarn -v  // 查看yarn 版本
yarn config list  // 查看yarn配置
yarn config get registry   // 查看当前yarn源
 
// 修改yarn源(此处为淘宝的源)
yarn config set registry https://registry.npmmirror.com

yarn的安装和使用(极其详细)_yarn安装-CSDN博客

  • 4
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值