npm

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

npm

npm

npm is the default package manager for Node.js. v11 (2025) introduces strict publishing rules and
npx
caching improvements.
npm是Node.js的默认包管理器。2025年发布的v11版本引入了严格的发布规则,并对
npx
缓存进行了优化。

When to Use

使用场景

  • Default: It comes with Node. Zero friction.
  • Compatibility: The standard
    package-lock.json
    is supported everywhere.
  • Publishing:
    npm publish
    is the canonical way to share JS code.
  • 默认选择:随Node.js一同安装,零学习门槛。
  • 兼容性强:标准的
    package-lock.json
    格式被所有环境支持。
  • 代码发布
    npm publish
    是分享JavaScript代码的标准方式。

Quick Start

快速开始

bash
npm init -y
npm install lodash
npm install --save-dev jest
bash
npm init -y
npm install lodash
npm install --save-dev jest

Monorepo

Monorepo

npm init -w packages/my-lib
undefined
npm init -w packages/my-lib
undefined

Core Concepts

核心概念

package.json

package.json

Manifest file. Scripts, dependencies, metadata.
清单文件,包含脚本、依赖项和元数据。

package-lock.json

package-lock.json

Locks dependency tree for reproducible builds. Commit this.
锁定依赖树以实现可复现的构建。请提交该文件到版本控制系统

Workspaces

Workspaces

Native monorepo support.
npm install
installs dependencies for root and all nested packages.
原生的monorepo支持。
npm install
会为根目录及所有嵌套包安装依赖。

Best Practices (2025)

2025年最佳实践

Do:
  • Use
    npm ci
    : For CI/CD pipelines. Faster and strict (fails if lockfile doesn't match).
  • Audit:
    npm audit
    to find vulnerabilities.
  • Use Scopes:
    @my-org/my-pkg
    to avoid name collisions.
Don't:
  • Don't mix managers: Don't use
    yarn
    in a repo with
    package-lock.json
    .
推荐做法
  • 使用
    npm ci
    :适用于CI/CD流水线,速度更快且严格(如果锁文件不匹配则会失败)。
  • 安全审计:使用
    npm audit
    查找漏洞。
  • 使用作用域:通过
    @my-org/my-pkg
    的命名方式避免名称冲突。
不推荐做法
  • 不要混合包管理器:在使用
    package-lock.json
    的仓库中不要使用
    yarn

References

参考资料