编辑
2024-07-20
未分类
00
请注意,本文编写于 49 天前,最后修改于 49 天前,其中某些信息可能已经过时。

目录

node相关包管理器的安装配置

镜像源

ini
# 中国科大 https://mirrors.ustc.edu.cn/help/index.html #清华 https://mirrors.tuna.tsinghua.edu.cn/

nvm 管理和切换node版本

安装nvm

mac安装

sh
brew install nvm

其他方式安装

https://github.com/nvm-sh/nvm

sh
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash #国内加速下载 git clone -b v0.39.1 https://gitee.com/mirrors/nvm.git && sh ./nvm/install.sh #curl -o- https://gitee.com/mirrors/nvm/raw/v0.39.1/install.sh | bash #国内加速下载 #curl -o- https://raw.staticdn.net/nvm-sh/nvm/v0.39.1/install.sh | bash

添加环境变量和 nvm node国内镜像

把如下内容添加到:(如果已存在就不需要添加)

MACOS : ~/.zshrc
linux: .bashrc

sh
export NVM_DIR="$HOME/.nvm" [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm [ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion # 国内镜像 # 国内镜像 node export NVM_NODEJS_ORG_MIRROR=https://npmmirror.com/mirrors/node/

应用并生效

MACOS : source ~/.zshrc
MACOS : source ~/.bashrc

nvm使用

查看所有可安装 Node 版本号

sh
#查看所有可安装 Node 版本号 nvm ls-remote #查看所有可安装 Node v17开头的版本好 nvm ls-remote |grep v17

安装最新版本

sh
nvm install node 安装指定版本 nvm install v17.9.1

查看已安装的所有版本

sh
nvm ls

切换node版本

sh
# 使用指定版本 nvm use 10.16.2 #使用最新版本 nvm use node

设置全局默认版本

sh
nvm alias default v18.16.0

查看默认版本

sh
# 输入查看当前默认版本 nvm ls ===================== v14.20.0 v17.9.1 v18.4.0 v18.5.0 -> v18.16.0 default -> v18.16.0

nvm的开启与停用

开启nvm,并设置使用最新node 版本

sh
nvm on

关闭nvm

此时 node 命令不可使用,如果再执行nvm use xxx会重新开起

sh
nvm off

设置下载代理

设置代理, 不加可选参数url,显示当前代理 将url设置为none则移除代理

sh
nvm proxy [url]

设置不同版本node存储目录,不写path查看当前node存储目录

sh
nvm root [path]

管理和切换node版本

sh
npm install -g n 或 npm install --global n

查看最新版

sh
n --stable

查看 版本

sh
npm -v node -v

安装最新版

sh
n stable

查看当前版

sh
node -v npm -v

sh
npm install npm@latest -g

nrm 切换 npm 镜像源

安装nrm

sh
npm install -g nrm

查看列表

sh
nrm ls

添加 中国镜像站源

sh
nrm add npmmirror registry https://registry.npmmirror.com

切换 中国镜像站源

sh
nrm use npmmirror

删除 中国镜像站源

sh
nrm del npmmirror

npm 管理

npm 镜像

sh
npm config set registry <https://registry.npmmirror.com> npm config set registry <https://registry.npmmirror.com>

安装模块

安装express模块

sh
# npm install express # 安装指定版本 npm install express@11.0.0 # # 安装但不写入package.json npm install express # # 安装并写入package.json的"dependencies"中 npm install express -S # 安装并写入package.json的"devDependencies"中 npm install express -D

全局安装模块

sh
npm install -g express #全局安装express模块

显示模块详情

sh
npm show express

已安装模块

sh
npm list

升级当前目录下的项目的所有模块

sh
npm update

升级当前目录下的项目的指定模块

sh
npm update express

升级全局安装的express模块

sh
npm update -g express

升级package.json中依赖包版本

方式一 升级到最新版本

sh
npm install --force 或 # 升级到最新版本 npm install

方式二 使用第三方

sh
npm install -g npm-check-updates ## # # 查看可更新包 ncu #更新package.json ncu -u

查看可更新模块

sh
npm outdated

删除指定的模块

sh
npm uninstall express

删除全局模块

sh
npm uninstall -g express

清除缓存

sh
npm cache clean --force npm cache clean -f

验证缓存

sh
npm cache verify

yarn

安装yarn

sh
npm install -g yarn

查看版本

sh
yarn --version

输出

sh
1.22.17

yarn 1 镜像

sh
yarn config set registry <https://registry.npmmirror.com> yarn config set registry <https://registry.npmmirror.com>

yarn 清除缓存

sh
yarn cache clean 或 npm cache clean --force

查看已缓存包的列表

sh
yarn cache list

cache缓存文件目录路径

sh
yarn cache dir

改变 yarn cache缓存文件目录路径

sh
mkdir -p /User/fox/.yarn/cache yarn config set cache-folder /User/fox/.yarn/cache 或者 yarn <command> --cache-folder <path> 例如 yarn install --cache-folder /User/fox/.yarn/cache

升级依赖

sh
yarn upgrade-interactive --latest

手动选择要升级的包 按空格键选择, a 键切换所有,i 键反选选择

yarn2

安装(需要线安装yarn1,然后切换yarn2)

sh
### 安装yarn1 npm install -g yarn ### 查看版本 yarn --version ### 输出 1.22.17 ### 升级到yarn>V2 yarn set version berry ### 查看版本 yarn --version ### 输出 4.1.1

yarn 会创建 .yarn 目录和 .yarnrc.yml 文件,只关注.yarnrc.yml

如果 yarn set version berry 报错 那么看文章最后部分 yarn2国内下载过慢解决

配置.yarnrc.yml

官方手册:https://www.yarnpkg.cn/configuration/yarnrc

默认内容有:

yaml
yarnPath: ".yarn/releases/yarn-berry.cjs"

如果已经设置 yarn2镜像库那么会有:

yaml
yarnPath: ".yarn/releases/yarn-berry.cjs" npmRegistryServer: "<https://registry.npmmirror.com>"

升级yarn依赖

sh
yarn up

执行该命令后,如果是yarn1项目或者npm项目,那么: node_modules被清空 多了.pnp.cjs 多了.pnp.loader..mjs .yarn目录下多了很多文件

目录说明

sh
|____.yarn Yarn 2的具体执行文件,安装的依赖等等 | |____releases | | |____yarn-berry.cjs 具体执行文件 | |____cache 安装的依赖,全部是压缩包 | | |____......... | |____install-state.gz | |____unplugged 是否具有安装脚本 | | |____......... |____.pnp.cjs |____.pnp.loader.mjs |____yarn.lock |____yarn-error.log |____.yarnrc.yml 具体配置文件

方式一

  • 只提交 .yarnrc.yml文件,其他人使用时候,执行yarn install 重新下载安装所有依赖

方式二:(团队版)

  • 提交 .yarnrc.yml.pnp.cjs,.pnp.loader.mjs文件和.yarn目录,其他人使用时候,不需要执行yarn install ,直接yarn dev运行即可。因为依赖已经下载安装了,这就是Yarn 2提出的零安装zero-install

方式三

  • 项目很多时候,依赖几乎完全一样的时候,在 .yarnrc.yml文件配置enableGlobalCache: true,提交 .yarnrc.yml文件,那么其他项目用到一样的依赖不在下载,会自动使用全局版本

enableGlobalCache : 是否启用全局缓存,默认不启用(所有缓存都存在.yarn/cache目录下)。

配置yarn2 设置 yarn镜像库

sh
yarn config set npmRegistryServer <https://registry.npmmirror.com>

yarn2 命令

安装yarn2依赖

sh
yarn install 或 yarn

添加依赖

sh
yarn add [package] [--dev|--peer]

删除依赖

sh
yarn remove [package]

更多

sh
yarn --help

yarn2国内下载过慢解决

sh
mkdir -p .yarn/releases && \ wget <https://repo.yarnpkg.com/3.2.0/packages/yarnpkg-cli/bin/yarn.js> -O ./.yarn/releases/yarn-berry.cjs && \ cat >> .yarnrc.yml <<EOF yarnPath: ".yarn/releases/yarn-berry.cjs" npmRegistryServer: "<https://registry.npmmirror.com>" EOF

4.1.1 这个版本号怎么来,请看 https://github.com/yarnpkg/berry/releases

最后执行

sh
yarn up 或者其他命令

更多 yarn2 请看 https://segmentfault.com/a/1190000040520326

pnpm 安装

官网 https://pnpm.io/

中文 https://pnpm.io/zh/

https://www.pnpm.cn/

脚本安装

windows

sh
iwr <https://get.pnpm.io/install.ps1> -useb | iex

linux mac

sh
curl -fsSL <https://get.pnpm.io/install.sh> | sh -

安装指定的版本

sh
curl -fsSL https://get.pnpm.io/install.sh | env PNPM_VERSION=<version> sh -

通过 npm 安装 pnpm

sh
npm install -g pnpm

通过 Homebrew 安装 pnpm

sh
brew install pnpm

通过 Scoop 安装 pnpm

sh
scoop install nodejs-lts pnpm

pnpm 升级 pnpm

升级pnpm方式一

sh
pnpm i -g pnpm

方式二

sh
pnpm i -g pnpm@指定版本号

查看版本

sh
pnpm -v

pnpm 命令

设置国内镜像

sh
pnpm config set registry <https://registry.npmmirror.com>

添加pnpm依赖

命令说明
pnpm add sax保存到 dependencies 配置项下
pnpm add -D sax保存到 devDependencies 配置项下
pnpm add -O sax保存到 optionalDependencies 配置项下
pnpm add -g sax安装软件包到全局环境中
pnpm add sax@next安装标记为 next 的版本
pnpm add sax@3.0.0安装指定版本 3.0.0

初始化依赖

sh
pnpm install 或 pnpm i

升级pnpm依赖

sh
pnpm up 或 pnpm up --latest 或 pnpm up xxx@版本号 例如 pnpm up foo@2

删除pnpm缓存

sh
pnpm store prune

store-dir 目录位置

默认值:
Windows 上: ~/AppData/Local/pnpm/store
macOS 上: ~/Library/pnpm/store
Linux 上: ~/.local/share/pnpm/store

pnpm server 相关命令

sh
pnpm server start pnpm server stop pnpm server status

参考资料

本文作者:任浪漫

本文链接:

版权声明:本博客所有文章除特别声明外,均采用 CC BY-NC-ND 4.0 许可协议。转载请注明出处!