系统新装 Node.js,对 NPM 进行配置,包括镜像源、全局依赖安装路径等。

查看原有配置

$ npm config ls
; cli configs
metrics-registry = "https://registry.npmjs.org/"
scope = ""
user-agent = "npm/6.14.12 node/v14.16.1 win32 x64"

; builtin config undefined
prefix = "C:\\Users\\CareyQ\\AppData\\Roaming\\npm"

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

配置镜像源

更换淘宝源

npm config set registry https://registry.npm.taobao.org

验证

$ npm config get registry
https://registry.npm.taobao.org/

设置依赖全局安装位置

首先在想要的位置创建两个文件夹,例如:

使用命令设置路径

$ npm set prefix D:\Environment\npm\npm_global
$ npm set cache D:\Environment\npm\npm_cache

验证

$ npm get prefix
D:\Environment\npm\npm_global
$ npm get cache
D:\Environment\npm\npm_cache

将全局路径添加环境变量中