# 2 种 NodeJS 包管理工具
# NPM
NPM 是 JavaScript 世界的包管理工具,并且是 Node.js 平台的默认包管理工具。 通过 npm 可以安装、共享、分发代码,管理项目依赖关系。
# Yarn
Yarn 是 Facebook 发布的,代替 NPM 的方案。目前最成熟的代替方案。
# 临时指定仓库路径
解决国内下载慢的问题
npm install xxx --registry=https://registry.npm.taobao.org
1
# 修改仓库配置
解决国内下载慢的问题
# 更换为淘宝源
npm config set registry https://registry.npm.taobao.org --global
npm config set disturl https://npm.taobao.org/dist --global
# node-sass bin
npm config set sass-binary-site https://npm.taobao.org/mirrors/node-sass
# phantomjs
npm config set phantomjs-cdnurl http://cnpmjs.org/downloads
1
2
3
4
5
6
7
2
3
4
5
6
7
# 更换为淘宝源
yarn config set registry https://registry.npm.taobao.org
# node-sass bin
yarn config set sass-binary-site https://npm.taobao.org/mirrors/node-sass
# phantomjs
yarn config set phantomjs-cdnurl http://cnpmjs.org/downloads
1
2
3
4
5
6
2
3
4
5
6
# NPM 私服
https://www.npmjs.com/package/verdaccio (opens new window)
npm install -g verdaccio --unsafe-perm
1
Verdaccio 默认是 localhost,要使用 ip 给其它电脑访问,可在 config.yaml 文件中添加一行 listen: 0.0.0.0:4873
# 修改 NPM 缓存配置
npm config get cache
1
npm config set cache "D:\npm-cache"
1
清除缓存:
npm cache clean -f
1
# 修改 Yarn 缓存配置
yarn cache list
1
Yarn 会在你的用户目录下开辟一块全局缓存用以保存下载的包。yarn cache list
用于列出所有已经缓存的包。
yarn cache dir
1
执行 yarn cache dir
命令会打印出当前的 yarn 全局缓存在哪里。
yarn cache clean
1
执行此命令将清除本地缓存。下次执行 yarn 或 yarn install
时将会重新填充缓存。
改变 yarn 的缓存路径
设置 cache-folder
的值用来改变缓存目录:
yarn config set cache-folder <path>
1
你还可以利用 --cache-folder
参数来指定缓存目录:
yarn <command> --cache-folder <path>
1
# 修改 NPM 全局安装时的路径
# 创建新目录
mkdir ~/.npm-global
npm config set prefix '~/.npm-global'
# 修改环境变量
export PATH=~/.npm-global/bin:$PATH
1
2
3
4
5
2
3
4
5
# Unix-like 系统修改 NPM 全局安装时的路径
# 创建新目录
mkdir ~/.npm-global
npm config set prefix '~/.npm-global'
# 修改环境变量
export PATH=~/.npm-global/bin:$PATH
1
2
3
4
5
2
3
4
5
# yarn 2.x 换源
注意:只支持项目级切换(需要切换到项目根目录)
yarn config set npmRegistryServer https://registry.npm.taobao.org
1
# yarn 2.x 使用 npm 兼容模式
新建 .yarnrc.yml
添加
nodeLinker: node-modules
1