在安装node.js使用npm命令之前,需要做一下基本配置,比如配置淘宝的CNPM,修改NPM/YARN的缓存路径以防止C盘被几十万的缓存文件弄爆炸。在此汇总一下,以下配置基于windows。
本文主要解决:npm/yarn/pnpm缓存清理、修改npm/yarn默认缓存目录(global & cache),修改npm/yarn默认源为淘宝源。
请注意,修改npm/yarn/pnpm默认缓存目录为我个人喜好,请根据实际情况操作。
官网汇总
PATH的添加
我们要将执行文件的目录,添加到系统变量中,这样才能支持那些包的命令行运行,其中包括:
- npm的global文件夹,比如
G:\temp\node-global
- yarn的global的bin文件夹,比如
G:\temp\yarn-global
- pnpm的global,比如
G:\temp\pnpm-global
添加到系统的PATH系统变量(环境变量)中。为避免错误,建议将新添加的系统变量提升到列表顶端,随后重启电脑。
如果不重启电脑的话,可能导致部分编辑器的集成命令行因没有同步系统变量而找不到执行文件。
注1:打开这两个文件夹看看,看看一堆.cmd文件在哪里。不在global就在global/bin。
注2:Windows 11 下的 Powershell 还要开一个权限:set-ExecutionPolicy RemoteSigned
npm & cnpm & yarn & tyarn & pnpm
注意:以下配置在npm 6.13.7
/ yarn 1.21.1
/ Windows 10
/ 2020年2月8日
测试通过,以下配置路径凡我没有加双引号的,请不要擅自加入;加了双引号的,请不要擅自删除或修改,反斜杠也一样。
#安装 yarn / tyarn / pnpm npm i yarn tyarn pnpm -g # 查看缓存目录 npm config get cache yarn cache dir # 清理缓存包 npm cache clean --force yarn cache clean # 设置npm缓存目录:修改默认在C盘的缓存,防止C盘过大(提前建立好缓存文件夹) npm config set prefix "G:\temp\node-global" npm config set cache "G:\temp\node-cache" # 配置Yarn缓存目录: 第一步 yarn config set prefix G:\temp\yarn-global yarn config set cache-folder G:\temp\yarn-cache yarn config set global-folder G:\temp\yarn-global # 第二步,打开%userprofile%,修改.yarnrc,添加新行: --global-folder "G:\\temp\\yarn-global" # 最后通过yarn global dir验证一下修改是否成功 # 查看当前源 npm get registry yarn config get registry # 设置镜像源 npm config set registry http://registry.npm.taobao.org/ yarn config set registry http://registry.npm.taobao.org/ # 安装淘宝的cnpm npm install -g cnpm --registry=https://registry.npm.taobao.org #pnpm pnpm config set global-dir G:\temp\pnpm-global pnpm config set cache-dir G:\temp\pnpm-cache pnpm config set store-dir G:\temp\pnpm-store
Node.js 的版本维护
使用 pnpm 作为 node 版本的管理工具
pnpm --global env use 18 pnpm --global env use lts
使用 nvm 作为 node 版本的管理工具,用 nvm 安装 node
https://github.com/coreybutler/nvm-windows
包的维护
用 npm-check-updates 工具对最新的包版本进行检查
https://www.npmjs.com/package/npm-check-updates
# 使用交互方式查看最新的包,选择性升级 # ncu 只会修改 package.json 文件,不会自动执行更新包的命令 ncu -i # yarn中global包升级 yarn global upgrade # yarn中特定包升级 yarn upgrade -lastest umi yarn upgrade umi@3.0.0
参考资料
https://classic.yarnpkg.com/en/docs/cli/global/