npm

npm

Config

Proxy

npm -g config set proxy http://127.0.0.1:1080
npm -g config set https-proxy http://127.0.0.1:1080

CDN (cnpm)

# Will automatically store your config to ~/.npmrc
#  registry=https://registry.npm.taobao.org
npm config set registry https://registry.npm.taobao.org

#Manually install package from registry
npm i --registry=https://registry.npm.taobao.org

注意:如果用 npm config 设置过了全局 cnpm 的 registry 后, npm login / npm publish 无法正常工作。需要恢复为官方 registry:

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

Full Taobao Mirror

npm set registry https://registry.npm.taobao.org \
&& yarn config set registry https://registry.npm.taobao.org \
&& npm set disturl https://npm.taobao.org/dist \
&& npm set chromedriver_cdnurl http://cdn.npm.taobao.org/dist/chromedriver \
&& npm set operadriver_cdnurl http://cdn.npm.taobao.org/dist/operadriver \
&& npm set phantomjs_cdnurl http://cdn.npm.taobao.org/dist/phantomjs \
&& npm set fse_binary_host_mirror https://npm.taobao.org/mirrors/fsevents \
&& npm set sass_binary_site http://cdn.npm.taobao.org/dist/node-sass \
&& npm set electron_mirror http://cdn.npm.taobao.org/dist/electron/

electron-builder

export ELECTRON_MIRROR=https://npm.taobao.org/mirrors/electron/;
export ELECTRON_BUILDER_BINARIES_MIRROR=http://npm.taobao.org/mirrors/electron-builder-binaries/;

Check license

npx license-checker --summary
npx license-compatibility-checker

Alternatives

yarn

yarn config set proxy http://127.0.0.1:1080
yarn config set https-proxy http://127.0.0.1:1080

Tips & Solutions

User "undefined"/"nobody" does not have permission to access the dir

"npm i -g" 全局安装需要包含需要编译 (node-gyp) 的原生模块的 package 时,需要加上 --unsafe 参数使用 root 用户身份执行命令。

npm i Error

根据经验 99% 的 npm 安装报错都是网络问题。

出错后首先清除缓存然后再重试。否则可能一直失败。

npm cache clean --force
rm -rf node_modules

Dirty patch

Use patch-package

# fix a bug in one of your dependencies
vim node_modules/some-package/brokenFile.js

# run patch-package to create a .patch file
npx patch-package some-package

# commit the patch file to share the fix with your team
git add patches/some-package+3.14.15.patch
git commit -m "fix brokenFile.js in some-package"

Install patch-package

npm i --save patch-package

Package.json:

 "scripts": {
+  "postinstall": "patch-package"
 }

Last update: 2023-10-08 07:02:47 UTC