npm
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
Chinesenpm
npm
npm is the default package manager for Node.js. v11 (2025) introduces strict publishing rules and caching improvements.
npxnpm是Node.js的默认包管理器。2025年发布的v11版本引入了严格的发布规则,并对缓存进行了优化。
npxWhen to Use
使用场景
- Default: It comes with Node. Zero friction.
- Compatibility: The standard is supported everywhere.
package-lock.json - Publishing: is the canonical way to share JS code.
npm publish
- 默认选择:随Node.js一同安装,零学习门槛。
- 兼容性强:标准的格式被所有环境支持。
package-lock.json - 代码发布:是分享JavaScript代码的标准方式。
npm publish
Quick Start
快速开始
bash
npm init -y
npm install lodash
npm install --save-dev jestbash
npm init -y
npm install lodash
npm install --save-dev jestMonorepo
Monorepo
npm init -w packages/my-lib
undefinednpm init -w packages/my-lib
undefinedCore 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.
installs dependencies for root and all nested packages.
npm install原生的monorepo支持。
会为根目录及所有嵌套包安装依赖。
npm installBest Practices (2025)
2025年最佳实践
Do:
- Use : For CI/CD pipelines. Faster and strict (fails if lockfile doesn't match).
npm ci - Audit: to find vulnerabilities.
npm audit - Use Scopes: to avoid name collisions.
@my-org/my-pkg
Don't:
- Don't mix managers: Don't use in a repo with
yarn.package-lock.json
推荐做法:
- 使用:适用于CI/CD流水线,速度更快且严格(如果锁文件不匹配则会失败)。
npm ci - 安全审计:使用查找漏洞。
npm audit - 使用作用域:通过的命名方式避免名称冲突。
@my-org/my-pkg
不推荐做法:
- 不要混合包管理器:在使用的仓库中不要使用
package-lock.json。yarn