NPM与Yarn的安装与配置
目录
1 Install NodeJS
1.1 Linux
1.1.1 Use nvm
https://github.com/nvm-sh/nvm?tab=readme-ov-file#git-install
如果无法稳定快速地访问 GitHub, 使用文件 https://raw.hellogithub.com/hosts
修改系统 hosts:
- Windows:
C:\Windows\System32\drivers\etc\hosts
- Linux:
/etc/hosts
1.1.2 Build from Source Code
https://github.com/nodesource/distributions
1.2 Windows
预构建安装程序: https://nodejs.org/zh-cn/download/prebuilt-installer/current
预构建二进制文件 (压缩包): https://nodejs.org/zh-cn/download/prebuilt-binaries/current
所有版本: https://nodejs.org/dist/
2 NPM
2.1 Install
npm 已被包含在 Node.js 中, 不必单独安装。
2.2 Config
2.2.1 Environment Variable
对于Windows:
- Node.js文件夹路径:
D:\NodeJS
。 - 创建文件夹
D:\NodeJS\node_cache
和D:\NodeJS\node_global
。 - 环境变量 → 系统变量 → PATH 中, 新建
D:\NodeJS
,D:\NodeJS\node_cache
和D:\NodeJS\node_global
。
2.2.2 Cache & Global
检查 npm 的缓存路径和全局安装路径:
npm config get cache
npm config get prefix
配置缓存路径和全局安装路径:
npm config set cache "D:\NodeJS\node_cache"
npm config set prefix "D:\NodeJS\node_global"
2.2.3 NPM Mirrors
检查当前源:
npm config get registry
更换镜像源:
# 腾讯云镜像
npm config set registry https://mirrors.cloud.tencent.com/npm/
# 淘宝镜像源
npm config set registry https://registry.npmmirror.com
# 默认官方源
npm config set registry https://registry.npmjs.org
3 Yarn
3.1 Install
3.1.1 Via NPM
npm install --global yarn
检查版本:
yarn --version
3.1.2 Via Official Package Repository
安装必要组件:
sudo apt-get update && sudo apt-get install -y apt-transport-https ca-certificates curl gnupg lsb-release
添加 GPG:
curl -fsSL https://dl.yarnpkg.com/debian/pubkey.gpg | sudo gpg --dearmor -o /etc/apt/keyrings/yarn.gpg
设置 Yarn 源:
echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/yarn.gpg] https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
安装 Yarn:
sudo apt-get update && sudo apt-get install -y yarn
检查版本:
yarn --version
3.2 Config
3.2.1 Environment Variable
对于 Windows:
- 创建文件夹
D:\NodeJS\yarn_cache
和D:\NodeJS\yarn_global
。 - 环境变量 → 系统变量 → PATH 中, 新建
D:\NodeJS\yarn_cache
和D:\NodeJS\yarn_global
。
3.2.2 Cache & Global
检查 yarn 的缓存路径和全局安装路径:
yarn config get cache-folder
yarn config get global-dir
配置缓存路径和全局安装路径:
yarn config set cache-folder "D:\NodeJS\yarn_cache"
yarn config set global-dir "D:\NodeJS\yarn_global"
3.2.3 Yarn Mirrors
检查当前源:
yarn config get registry
更换镜像源:
# 腾讯云镜像
yarn config set registry https://mirrors.cloud.tencent.com/npm/
# 淘宝镜像源
yarn config set registry https://registry.npmmirror.com
# 默认官方源
yarn config set registry https://registry.yarnpkg.com