• 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

    操作全过程:

    1. Windows PowerShell
    2. 版权所有 (C) Microsoft Corporation。保留所有权利。
    3. 尝试新的跨平台 PowerShell https://aka.ms/pscore6
    4. PS C:\Windows\system32> nvm -v
    5. 1.1.12
    6. PS C:\Windows\system32> nvm list
    7. No installations recognized.
    8. PS C:\Windows\system32> nvm list available
    9. | CURRENT | LTS | OLD STABLE | OLD UNSTABLE |
    10. |--------------|--------------|--------------|--------------|
    11. | 21.5.0 | 20.11.0 | 0.12.18 | 0.11.16 |
    12. | 21.4.0 | 20.10.0 | 0.12.17 | 0.11.15 |
    13. | 21.3.0 | 20.9.0 | 0.12.16 | 0.11.14 |
    14. | 21.2.0 | 18.19.0 | 0.12.15 | 0.11.13 |
    15. | 21.1.0 | 18.18.2 | 0.12.14 | 0.11.12 |
    16. | 21.0.0 | 18.18.1 | 0.12.13 | 0.11.11 |
    17. | 20.8.1 | 18.18.0 | 0.12.12 | 0.11.10 |
    18. | 20.8.0 | 18.17.1 | 0.12.11 | 0.11.9 |
    19. | 20.7.0 | 18.17.0 | 0.12.10 | 0.11.8 |
    20. | 20.6.1 | 18.16.1 | 0.12.9 | 0.11.7 |
    21. | 20.6.0 | 18.16.0 | 0.12.8 | 0.11.6 |
    22. | 20.5.1 | 18.15.0 | 0.12.7 | 0.11.5 |
    23. | 20.5.0 | 18.14.2 | 0.12.6 | 0.11.4 |
    24. | 20.4.0 | 18.14.1 | 0.12.5 | 0.11.3 |
    25. | 20.3.1 | 18.14.0 | 0.12.4 | 0.11.2 |
    26. | 20.3.0 | 18.13.0 | 0.12.3 | 0.11.1 |
    27. | 20.2.0 | 18.12.1 | 0.12.2 | 0.11.0 |
    28. | 20.1.0 | 18.12.0 | 0.12.1 | 0.9.12 |
    29. | 20.0.0 | 16.20.2 | 0.12.0 | 0.9.11 |
    30. | 19.9.0 | 16.20.1 | 0.10.48 | 0.9.10 |
    31. This is a partial list. For a complete list, visit https://nodejs.org/en/download/releases
    32. PS C:\Windows\system32> nvm install 14.17
    33. Downloading node.js version 14.17.6 (64-bit)...
    34. Complete
    35. Creating C:\env\nvm\temp
    36. Downloading npm version 6.14.15... Complete
    37. Installing npm v6.14.15...
    38. Installation complete. If you want to use this version, type
    39. nvm use 14.17.6
    40. PS C:\Windows\system32>
    41. PS C:\Windows\system32>
    42. PS C:\Windows\system32> nvm list
    43. 14.17.6
    44. PS C:\Windows\system32> nvm use 14
    45. Now using node v14.17.6 (64-bit)
    46. PS C:\Windows\system32> nvm list
    47. * 14.17.6 (Currently using 64-bit executable)
    48. PS C:\Windows\system32> node -v
    49. v14.17.6
    50. PS C:\Windows\system32> npm -v
    51. 6.14.15
    52. PS C:\Windows\system32> npm install -g yarn
    53. > yarn@1.22.21 preinstall C:\env\nodejs\node_modules\yarn
    54. > :; (node ./preinstall.js > /dev/null 2>&1 || true)
    55. C:\env\nodejs\yarn -> C:\env\nodejs\node_modules\yarn\bin\yarn.js
    56. C:\env\nodejs\yarnpkg -> C:\env\nodejs\node_modules\yarn\bin\yarn.js
    57. + yarn@1.22.21
    58. added 1 package in 2.784s
    59. PS C:\Windows\system32> npm config ls
    60. ; cli configs
    61. metrics-registry = "https://registry.npmjs.org/"
    62. scope = ""
    63. user-agent = "npm/6.14.15 node/v14.17.6 win32 x64"
    64. ; node bin location = C:\env\nodejs\node.exe
    65. ; cwd = C:\Windows\system32
    66. ; HOME = C:\Users\admin
    67. ; "npm config ls -l" to show all defaults.
    68. PS C:\Windows\system32> yarn config list
    69. yarn : 无法加载文件 C:\env\nodejs\yarn.ps1,因为在此系统上禁止运行脚本。有关详细信息,请参阅 https:/go.microsoft.com/fw
    70. link/?LinkID=135170 中的 about_Execution_Policies。
    71. 所在位置 行:1 字符: 1
    72. + yarn config list
    73. + ~~~~
    74. + CategoryInfo : SecurityError: (:) [],PSSecurityException
    75. + FullyQualifiedErrorId : UnauthorizedAccess
    76. PS C:\Windows\system32> Set-ExecutionPolicy Unrestricted
    77. 执行策略更改
    78. 执行策略可帮助你防止执行不信任的脚本。更改执行策略可能会产生安全风险,如 https:/go.microsoft.com/fwlink/?LinkID=135170
    79. 中的 about_Execution_Policies 帮助主题所述。是否要更改执行策略?
    80. [Y] 是(Y) [A] 全是(A) [N] 否(N) [L] 全否(L) [S] 暂停(S) [?] 帮助 (默认值为“N”): y
    81. PS C:\Windows\system32> yarn config list
    82. yarn config v1.22.21
    83. info yarn config
    84. {
    85. 'version-tag-prefix': 'v',
    86. 'version-git-tag': true,
    87. 'version-commit-hooks': true,
    88. 'version-git-sign': false,
    89. 'version-git-message': 'v%s',
    90. 'init-version': '1.0.0',
    91. 'init-license': 'MIT',
    92. 'save-prefix': '^',
    93. 'bin-links': true,
    94. 'ignore-scripts': false,
    95. 'ignore-optional': false,
    96. registry: 'https://registry.yarnpkg.com',
    97. 'strict-ssl': true,
    98. 'user-agent': 'yarn/1.22.21 npm/? node/v14.17.6 win32 x64'
    99. }
    100. info npm config
    101. {}
    102. Done in 0.10s.
    103. PS C:\Windows\system32> yarn config get registry
    104. https://registry.yarnpkg.com
    105. PS C:\Windows\system32> yarn config set registry https://registry.npmmirror.com
    106. yarn config v1.22.21
    107. success Set "registry" to "https://registry.npmmirror.com".
    108. Done in 0.09s.
    109. PS C:\Windows\system32> yarn config get registry
    110. https://registry.npmmirror.com
    111. 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:

    1. root: D:\env\nvm
    2. path: D:\env\nodejs
    3. node_mirror: https://npmmirror.com/mirrors/node/
    4. 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不好用)

    1. PS C:\Users\admin> npm config set prefix "D:\env\nvm\npm"
    2. PS C:\Users\admin> npm install npm -g --registry=https://registry.npmmirror.com
    3. D:\env\nvm\npm\npm -> D:\env\nvm\npm\node_modules\npm\bin\npm-cli.js
    4. D:\env\nvm\npm\npx -> D:\env\nvm\npm\node_modules\npm\bin\npx-cli.js
    5. 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"})
    6. npm WARN notsup Not compatible with your version of node/npm: npm@10.2.2
    7. + npm@10.2.2
    8. 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全局环境是比较干净的。

    1. PS C:\Users\admin> npm config ls
    2. ; cli configs
    3. metrics-registry = "https://registry.npmmirror.com/"
    4. scope = ""
    5. user-agent = "npm/6.14.15 node/v14.17.6 win32 x64"
    6. ; userconfig C:\Users\admin\.npmrc
    7. prefix = "D:\\env\\nvm\\npm6_14_15"
    8. registry = "https://registry.npmmirror.com/"
    9. ; node bin location = D:\env\nodejs\node.exe
    10. ; cwd = C:\Users\admin
    11. ; HOME = C:\Users\admin
    12. ; "npm config ls -l" to show all defaults.
    13. PS C:\Users\admin> npm list
    14. 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!
    15. C:\Users\admin
    16. +-- nrm@1.2.6
    17. | +-- chalk@4.1.2
    18. | | +-- ansi-styles@4.3.0
    19. | | | `-- color-convert@2.0.1
    20. | | | `-- color-name@1.1.4
    21. | | `-- supports-color@7.2.0
    22. | | `-- has-flag@4.0.0
    23. | +-- commander@8.3.0
    24. | +-- ini@2.0.0
    25. | +-- node-fetch@2.6.6
    26. | | `-- whatwg-url@5.0.0
    27. | | +-- tr46@0.0.3
    28. | | `-- webidl-conversions@3.0.1
    29. | `-- open@8.4.2
    30. | +-- define-lazy-prop@2.0.0
    31. | +-- is-docker@2.2.1
    32. | `-- is-wsl@2.2.0
    33. | `-- is-docker@2.2.1 deduped
    34. `-- 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国内源镜像

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

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

  • 相关阅读:
    Java设计模式之组合模式
    大数据基础设施搭建 - Spark
    使用setoolkit进行社会工程学攻击获取账号密码
    git容易出问题的命令
    uniapp实现单选框卡片选择器,支持微信小程序、H5等多端
    BootStrap在Vue中的安装使用详细教程
    【华为HCIP | 职业认证考试】821每日一刷
    蓝桥杯嵌入式STM32G431RBT6的学习(总大纲)(HAL库学习)板子介绍
    Hexagon_V65_Programmers_Reference_Manual(27)
    2022年“网络安全”赛项驻马店市赛选拔赛 任务书
  • 原文地址:https://blog.csdn.net/qq_27361945/article/details/134159196