npm 常用命令

发布时间: 更新时间: 总字数:1572 阅读时间:4m 作者:IP:上海 网址
专栏文章
  1. PM2: Node.js 进程管理
  2. Bun: 运行JavaScript 和TypeScript 应用程序的集成工具包
  3. Yarn JavaScript 包管理器介绍
  4. nodejs 版本管理
  5. NPM Registry Proxy 配置
  6. npm 常用命令(当前)
  7. 使用 forever 运行 nodejs 应用

npm 其实是 Node.js 的包管理工具(node package manager),本文介绍 npm 常用命令。

安装

安装 nodejs

  • mac
brew install node

验证:

node -v
npm -v

npm

npm 其实是 Node.js 的包管理工具(package manager)。

查看配置:

npm config ls -l

手动切换

npm config set registry https://registry.npmmirror.com/

pnpm

pNpM 快速的,节省磁盘空间的包管理工具

  • 安装 npm install -g pnpm
  • 通过变量 global-dir 指定储存全局依赖的目录,Linux 默认为 ~/.local/share/pnpm/global参考

cnpm

npm 源由于被墙,可以试用淘宝的 nodejs 源。使用我们定制的 cnpm (gzip 压缩支持) 命令行工具代替默认的 npm:

$ npm install -g cnpm --registry=https://registry.npmmirror.com

或者你直接通过添加 npm 参数 alias 一个新命令:

alias cnpm="npm --registry=https://registry.npmmirror.com \
--cache=$HOME/.npm/.cache/cnpm \
--disturl=https://npmmirror.com/package/dist \
--userconfig=$HOME/.cnpmrc"
# Or alias it in .bashrc or .zshrc
$ echo '\n#alias for cnpm\nalias cnpm="npm --registry=https://registry.npmmirror.com \
  --cache=$HOME/.npm/.cache/cnpm \
  --disturl=https://npmmirror.com/package/dist \
  --userconfig=$HOME/.cnpmrc"' >> ~/.zshrc && source ~/.zshrc

同步模块直接通过  sync  命令马上同步一个模块, 只有  cnpm  命令行才有此功能:

$ cnpm sync express

安装模块

从 registry.npmmirror.com 安装所有模块. 当安装的时候发现安装的模块还没有同步过来, 淘宝 NPM 会自动在后台进行同步, 并且会让你从官方 NPM registry.npmjs.org 进行安装. 下次你再安装这个模块的时候, 就会直接从 淘宝 NPM 安装了.

$ cnpm install [name]

nrm

nrm(NPM registry manager) 是 npm 的镜像源管理工具,用来快速切换 npm 源

# 安装
npm i -g nrm

# 帮助
nrm -h

# 版本
nrm -V

# 查看所有源
$ nrm ls
  npm ---------- https://registry.npmjs.org/
  yarn --------- https://registry.yarnpkg.com/
  tencent ------ https://mirrors.cloud.tencent.com/npm/
  cnpm --------- https://r.cnpmjs.org/
  taobao ------- https://registry.npmmirror.com/
  npmMirror ---- https://skimdb.npmjs.com/registry/

 # 切换源 nrm use <registry_name>
 nrm use taobao

 # 添加源
nrm add <registry_name> <url>

# 删除源
$ nrm del <registry_name>

# 测试源

$ nrm test <registry_name>

yarn & tyarn

  • yarn 支持并行下载的,下载速度很快
  • tyarn 使用 npmmirror.com 作为源,由国内开发
npm install yarn tyarn -g

Yarn (Yet Another Resource Negotiator)

Yarn 是由 Facebook(现 Meta)、Google、Exponent 和 Tilde 共同在 2016 年发布的包管理器。

  • 诞生背景:在 2016 年左右,当时的 npm(v3 时代)存在很多痛点,比如安装速度非常慢、依赖嵌套过深、以及没有版本锁定机制导致“在我的电脑上能跑,在你的电脑上跑不了”的问题。为了解决这些问题,Facebook 开发了 Yarn。
  • 核心特性(初创时的优势)
    • 极快的安装速度:Yarn 引入了并行下载机制,而当时的 npm 是串行下载的。
    • yarn.lock:Yarn 首创了 lock 文件机制,严格锁定所有依赖的精确版本,保证团队协作时的一致性。
    • 离线缓存:如果之前安装过某个包,Yarn 会将其缓存在本地,下次安装时无需网络即可直接从缓存提取。
  • Yarn 的演进 (Yarn Berry / Yarn 2+)
    • 目前的 Yarn 分为 Classic (v1) 和 Berry (v2+) 两个大版本。Yarn Berry 引入了极其先进的 PnP (Plug’n’Play,即插即用) 特性,彻底抛弃了臃肿的 node_modules 目录,并支持 Zero-Installs(零安装),但由于较为激进,生态兼容性仍在不断适配中。

npm 与 Yarn 的命令对比

日常开发中,两者的命令非常相似,但也有些许区别:

操作 npm 命令 Yarn (v1) 命令
初始化项目 npm init yarn init
安装所有依赖 npm install (或 npm i) yarn (或 yarn install)
安装单个/多个包 npm install <pkg> yarn add <pkg>
安装开发环境依赖 npm install <pkg> --save-dev yarn add <pkg> --dev (或 -D)
全局安装包 npm install -g <pkg> yarn global add <pkg>
移除某个包 npm uninstall <pkg> yarn remove <pkg>
更新包 npm update <pkg> yarn upgrade <pkg>
运行 package.json 脚本 npm run <script> yarn <script>
执行一次性命令 npx <cmd> yarn dlx <cmd> (Yarn 2+) 或直接 npx

npm 常用命令

npm 的命令适用于 cnpm、yarn 和 tyarn

  • 帮助,如果要单独查看 install 命令的帮助,可以使用的 npm help install
npm help
  • 安装nodejs的依赖包
npm install <name>

例如:npm install express 就会默认安装express的最新版本,也可以通过在后面加版本号的方式安装指定版本,如npm install express@3.0.6

  • 将包安装到全局环境中
npm install <name> -g

但是代码中,直接通过 require()的方式是没有办法调用全局安装的包的。全局的安装是供命令行使用的,就好像全局安装了 vmarket 后,就可以在命令行中直接运行 vm 命令

  • 安装的同时,将信息写入 package.json 中
npm install <name> --save

项目路径中如果有package.json文件时,直接使用npm install方法就可以根据 dependencies 配置安装所有的依赖包

这样代码提交到 github 时,就不用提交 node_modules 这个文件夹了。

  • 会引导你创建一个 package.json 文件,包括名称、版本、作者这些信息等
npm init
  • 移除
npm remove <name>

-更新

npm update <name>
  • 列出当前安装的了所有包
npm ls
  • 查看当前包的安装路径
npm root
  • 查看全局的包的安装路径
npm root -g

F&Q

fetch headers ssl err

  • 错误日志
gyp ERR! stack FetchError: request to https://nodejs.org/download/...headers.tar.gz failed, reason: unable to get local issuer certificate
  • 解决方式
cd /tmp && wget https://nodejs.org/download/release/v16.13.2/node-v16.13.2-headers.tar.gz
npm config set tarball /tmp/node-v16.13.2-headers.tar.gz
npm ci