npm
$ npm 官方出品 中风险随 Node.js 内置的标准命令行工具,用于管理 package.json、package-lock.json、脚本与包仓库。在 ls、outdated、audit、view、install --dry-run 与 publish --dry-run 上均原生支持 --json,并可通过 --ignore-scripts 与 npm ci 实现确定性安装。
npm install -g npm 与 node_modules 及 package-lock.json 紧密绑定的本地 Node.js 包与脚本管理 CLI。
Agent 友好度体检单
本地探针实测证据抓取的 --help 摘要
npm <command>
Usage:
npm install install all the dependencies in your project
npm install <foo> add the <foo> dependency to your project
npm test run this project's tests
npm run <foo> run the script named <foo>
npm <command> -h quick help on <command>
npm -l display usage info for all commands
npm help <term> search for help on <term>
npm help npm more involved overview
All commands:
access, adduser, approve-scripts, audit, bugs, cache, ci,
completion, config, dedupe, deny-scripts, deprecate, diff,
dist-tag, docs, doctor, edit, exec, explain, explore,
find-dupes, fund, get, help, help-search, init, install,
install-ci-test, install-scripts, install-test, link, ll,
login, logout, ls, org, outdated, owner, pack, ping, pkg,
prefix, profile, prune, publish, query, rebuild, repo,
restart, root, run, sbom, search, set, shrinkwrap, stage,
star, stars, start, stop, team, test, token, trust,
undeprecate, uninstall, unpublish, unstar, update, version,
view, whoami
Specify configs in the ini-formatted file:
/Users/pcyan/.npmrc
or on the command line via: npm <command> --key=value
More configuration info: npm help config
Configuration fields: npm help 7 config
[email protected] /opt/homebrew/Cellar/node@24/24.22.0/lib/node_modules/npm FCRS-1.0 六维评分拆解
ls、audit、outdated、view、install 与 publish 广泛支持 --json,并具备 --dry-run、--no-color、--no-fund 与 -y。
- Native
--jsonand--dry-runon core commands - Deterministic
npm cimode
支持 --dry-run 与 --ignore-scripts,但默认 npm install 会执行依赖包中的 preinstall/postinstall 任意脚本。
- Lifecycle script execution risk mitigated by
--ignore-scripts
随 Node.js 安装包预装在 macOS、Linux 与 Windows 全平台。
- Bundled by default with Node.js
Node.js 生态最通用的包管理与脚本执行入口(npm run、npm exec)。
- Standard registry and workspace tooling
由 GitHub/npm 官方团队(npm/cli)以 Artistic-2.0 协议维护。
- Official
npm/clirepository
docs.npmjs.com 与 npm help <cmd> 提供完整命令手册。
- Official documentation at docs.npmjs.com/cli
安全边界、鉴权与放权指南
从 ~/.npmrc(_authToken)或 NPM_TOKEN 环境变量读取仓库令牌;本地只读命令无需认证。
Artistic-2.0 开源 CLI;公共 npm 仓库拉取与开源包发布均免费。
# npm Agent Rules - Prefer `npm ci --ignore-scripts` over `npm install` on existing repos to avoid lockfile drift and untrusted `postinstall` scripts. - Always preview with `npm publish --dry-run` and never publish without explicit user confirmation.
Agent 高频安全命令配方
只读查询(安全推荐)
npm ls --depth=0 --json && npm audit --json
以结构化 JSON 输出顶层已安装依赖及已知漏洞公告。
npm install --dry-run --json
模拟依赖解析并以 JSON 返回预计新增或更新的包列表。
受控写入与预演
npm ci --ignore-scripts --no-audit --no-fund
严格依据 package-lock.json 还原 node_modules,同时阻断 postinstall 脚本执行。
核心优势
- +
--json与--dry-run贯穿依赖查询、安装预演与发布预检。 - + 随 Node.js 预装且可用
npm ci严格复现锁文件状态。
注意事项与踩坑点
- ! 默认
npm install会触发postinstall生命周期脚本,处理不受信任项目需显式加--ignore-scripts。 - ! 磁盘安装速度与幽灵依赖隔离性弱于
pnpm。