跨境派

跨境派

跨境派,专注跨境行业新闻资讯、跨境电商知识分享!

当前位置:首页 > 卖家故事 > nodejs国内镜像及切换版本工具nvm

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

时间:2024-04-23 17:30:25 来源:网络cs 作者:付梓 栏目:卖家故事 阅读:

标签: 版本  工具 
阅读本书更多章节>>>>

使用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/pscore6PS C:\Windows\system32> nvm -v1.1.12PS C:\Windows\system32> nvm listNo 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/releasesPS C:\Windows\system32> nvm install 14.17Downloading node.js version 14.17.6 (64-bit)...CompleteCreating C:\env\nvm\tempDownloading npm version 6.14.15... CompleteInstalling npm v6.14.15...Installation complete. If you want to use this version, typenvm use 14.17.6PS C:\Windows\system32>PS C:\Windows\system32>PS C:\Windows\system32> nvm list    14.17.6PS C:\Windows\system32> nvm use 14Now 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 -vv14.17.6PS C:\Windows\system32> npm -v6.14.15PS 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.jsC:\env\nodejs\yarnpkg -> C:\env\nodejs\node_modules\yarn\bin\yarn.js+ yarn@1.22.21added 1 package in 2.784sPS C:\Windows\system32> npm config ls; cli configsmetrics-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 listyarn : 无法加载文件 C:\env\nodejs\yarn.ps1,因为在此系统上禁止运行脚本。有关详细信息,请参阅 https:/go.microsoft.com/fwlink/?LinkID=135170 中的 about_Execution_Policies。所在位置 行:1 字符: 1+ yarn config list+ ~~~~    + CategoryInfo          : SecurityError: (:) [],PSSecurityException    + FullyQualifiedErrorId : UnauthorizedAccessPS 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”): yPS C:\Windows\system32> yarn config listyarn config v1.22.21info 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 registryhttps://registry.yarnpkg.comPS C:\Windows\system32> yarn config set registry https://registry.npmmirror.comyarn config v1.22.21success Set "registry" to "https://registry.npmmirror.com".Done in 0.09s.PS C:\Windows\system32> yarn config get registryhttps://registry.npmmirror.comPS 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\nvmpath: D:\env\nodejsnode_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.comD:\env\nvm\npm\npm -> D:\env\nvm\npm\node_modules\npm\bin\npm-cli.jsD:\env\nvm\npm\npx -> D:\env\nvm\npm\node_modules\npm\bin\npx-cli.jsnpm 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.2added 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 configsmetrics-registry = "https://registry.npmmirror.com/"scope = ""user-agent = "npm/6.14.15 node/v14.17.6 win32 x64"; userconfig C:\Users\admin\.npmrcprefix = "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 listnpm 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博客

阅读本书更多章节>>>>

本文链接:https://www.kjpai.cn/gushi/2024-04-23/161561.html,文章来源:网络cs,作者:付梓,版权归作者所有,如需转载请注明来源和作者,否则将追究法律责任!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。

文章评论