Skip to content

2 种 NodeJS 包管理工具的仓库和缓存配置笔记

2 种 NodeJS 包管理工具

NPM

NPM 是 JavaScript 世界的包管理工具,并且是 Node.js 平台的默认包管理工具。 通过 npm 可以安装、共享、分发代码,管理项目依赖关系。

Yarn

Yarn 是 Facebook 发布的,代替 NPM 的方案。目前最成熟的代替方案。

临时指定仓库路径

解决国内下载慢的问题

bash
npm install xxx --registry=https://registry.npm.taobao.org

修改仓库配置

解决国内下载慢的问题

bash
## 更换为淘宝源
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
bash
## 更换为淘宝源
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

NPM 私服

https://www.npmjs.com/package/verdaccio

bash
npm install -g verdaccio --unsafe-perm

Verdaccio 默认是 localhost,要使用 ip 给其它电脑访问,可在 config.yaml 文件中添加一行 listen: 0.0.0.0:4873

修改 NPM 缓存配置

bash
npm config get cache
bash
npm config set cache "D:\npm-cache"

清除缓存:

bash
npm cache clean -f

修改 Yarn 缓存配置

bash
yarn cache list

Yarn 会在你的用户目录下开辟一块全局缓存用以保存下载的包。yarn cache list 用于列出所有已经缓存的包。

bash
yarn cache dir

执行 yarn cache dir 命令会打印出当前的 yarn 全局缓存在哪里。

bash
yarn cache clean

执行此命令将清除本地缓存。下次执行 yarn 或 yarn install 时将会重新填充缓存。

改变 yarn 的缓存路径

设置 cache-folder 的值用来改变缓存目录:

bash
yarn config set cache-folder <path>

你还可以利用 --cache-folder 参数来指定缓存目录:

bash
yarn <command> --cache-folder <path>

修改 NPM 全局安装时的路径

bash
## 创建新目录
mkdir ~/.npm-global
npm config set prefix '~/.npm-global'
## 修改环境变量
export PATH=~/.npm-global/bin:$PATH

Unix-like 系统修改 NPM 全局安装时的路径

bash
## 创建新目录
mkdir ~/.npm-global
npm config set prefix '~/.npm-global'
## 修改环境变量
export PATH=~/.npm-global/bin:$PATH

yarn 2.x 换源

注意:只支持项目级切换(需要切换到项目根目录)

bash
yarn config set npmRegistryServer https://registry.npm.taobao.org

yarn 2.x 使用 npm 兼容模式

新建 .yarnrc.yml

添加

yaml
nodeLinker: node-modules

最后编辑时间:

Version 4.0 (framework-1.1.4)