本节总结了 npm 命令行工具的使用方法。
该节 Commands 是基于npm@9.6.6。
npm access
Set access level on published packages.
Usage:
npm access public [<package>]npm access restricted [<package>]npm access grant <read-only|read-write> <scope:team> [<package>]npm access revoke <scope:team> [<package>]npm access 2fa-required [<package>]npm access 2fa-not-required [<package>]npm access ls-packages [<user>|<scope>|<scope:team>]npm access ls-collaborators [<package> [<user>]]npm access edit [<package>]
Options:
[--registry <registry>][--otp <otp>]
npm adduser
Add a registry user account.
Usage:
npm adduser
Options:
[--registry <registry>][--scope <@scope>]
npm audit
Run a security audit.
Usage:
npm audit进行安全漏洞检测npm audit fix进行安全漏洞检测,并进行修复
Options:
[--audit-level <info|low|moderate|high|critical|none>][--dry-run][-f|--force][--json][--package-lock-only][--omit <dev|optional|peer> [--omit <dev|optional|peer> ...]][-w|--workspace <workspace-name> [-w|--workspace <workspace-name> ...]][-ws|--workspaces][--include-workspace-root]
npm bugs
Report bugs for a package in a web browser.
Usage:
npm bugs [<pkgname>]
Options:
[--no-browser|--browser <browser>][--registry <registry>]
npm cache
Manipulates packages cache.
Usage:
npm cache add <tarball file>npm cache add <folder>npm cache add <tarball url>npm cache add <git url>npm cache add <name>@<version>npm cache clean [<key>]npm cache ls [<name>@<version>]npm cache verify
Options:
[--cache <cache>]
npm ci
Install a project with a clean slate.
Usage:
npm ci
Options:
[--no-audit][--ignore-scripts][--script-shell <script-shell>]
npm completion
Tab Completion for npm.
Usage:
npm completion
npm config
Manage the npm configuration files.
Usage:
npm config set <key>=<value> [<key>=<value> ...]npm config get [<key> [<key> ...]]npm config delete <key> [<key> ...]npm config list [--json]npm config edit
Options:
[--json][-g|--global][--editor <editor>][-L|--location <global|user|project>][-l|--long]
npm dedupe
Reduce duplication in the package tree.
Usage:
npm dedupe
Options:
[--global-style][--legacy-bundling][--strict-peer-deps][--no-package-lock][--omit <dev|optional|peer> [--omit <dev|optional|peer> ...]][--ignore-scripts][--no-audit][--no-bin-links][--no-fund][--dry-run][-w|--workspace <workspace-name> [-w|--workspace <workspace-name> ...]][-ws|--workspaces][--include-workspace-root]
npm deprecate
Deprecate a version of a package.
Usage:
npm deprecate <pkg>[@<version>] <message>
Options:
[--registry <registry>][--otp <otp>]
npm diff
The registry diff command.
Usage:
npm diff [...<paths>]
Options:
[--diff <pkg-name|spec|version> [--diff <pkg-name|spec|version> ...]][--diff-name-only][--diff-unified <number>][--diff-ignore-all-space][--diff-no-prefix][--diff-src-prefix <path>][--diff-dst-prefix <path>][--diff-text][-g|--global][--tag <tag>][-w|--workspace <workspace-name> [-w|--workspace <workspace-name> ...]][-ws|--workspaces][--include-workspace-root]
npm dist-tag
Modify package distribution tags.
Usage:
npm dist-tag add <pkg>@<version> [<tag>]npm dist-tag rm <pkg> <tag>npm dist-tag ls [<pkg>]
Options:
[-w|--workspace <workspace-name> [-w|--workspace <workspace-name> ...]][-ws|--workspaces][--include-workspace-root]
npm docs
Open documentation for a package in a web browser.
Usage:
npm docs [<pkgname> [<pkgname> ...]]
Options:
[--no-browser|--browser <browser>][--registry <registry>][-w|--workspace <workspace-name> [-w|--workspace <workspace-name> ...]][-ws|--workspaces][--include-workspace-root]
npm doctor
Check your npm environment.
Usage:
npm doctor
Options:
[--registry <registry>]
npm edit
Edit an installed package.
Usage:
npm edit <pkg>[/<subpkg>...]
Options:
[--editor <editor>]
npm exec
Run a command from a local or remote npm package.
Usage:
npm exec -- <pkg>[@<version>] [args...]npm exec --package=<pkg>[@<version>] -- <cmd> [args...]npm exec -c '<cmd> [args...]'npm exec --package=foo -c '<cmd> [args...]'
Options:
[--package <pkg>[@<version>][--package <pkg>[@<version>] ...]][-c|--call <call>][-w|--workspace <workspace-name> [-w|--workspace <workspace-name> ...]][-ws|--workspaces][--include-workspace-root]
npm explain
Explain installed packages.
Usage:
npm explain <folder | specifier>
Options:
[--json][-w|--workspace <workspace-name> [-w|--workspace <workspace-name> ...]]
npm explore
Browse an installed package.
Usage:
npm explore <pkg> [ -- <command>]
Options:
[--shell <shell>]
npm find-dupes
Find duplication in the package tree.
Usage:
npm find-dupes
Options:
[--global-style][--legacy-bundling][--strict-peer-deps][--no-package-lock][--omit <dev|optional|peer> [--omit <dev|optional|peer> ...]][--ignore-scripts][--no-audit][--no-bin-links][--no-fund][-w|--workspace <workspace-name> [-w|--workspace <workspace-name> ...]][-ws|--workspaces][--include-workspace-root]
npm fund
Retrieve funding information.
Usage:
npm fund [[<@scope>/]<pkg>]
Options:
[--json][--no-browser|--browser <browser>][--no-unicode][-w|--workspace <workspace-name> [-w|--workspace <workspace-name> ...]][--which <fundingSourceNumber>]
npm help
Get help on npm.
Usage:
npm help <term> [<terms..>]
Options:
[--viewer <viewer>]
npm help-search
Search npm help documentation.
Usage:
npm help-search <text>
Options:
[-l|--long]
npm hook
Manage registry hooks.
Usage:
npm hook add <pkg> <url> <secret> [--type=<type>]npm hook ls [pkg]npm hook rm <id>npm hook update <id> <url> <secret>
Options:
[--registry <registry>] [--otp <otp>]
npm init
Create a package.json file.
Usage:
npm init [--force|-f|--yes|-y|--scope]npm init <@scope> (same asnpx <@scope>/create)npm init [<@scope>/]<name> (same as npx [<@scope>/]create-<name>)
Options:
[-y|--yes][-f|--force][-w|--workspace <workspace-name> [-w|--workspace <workspace-name> ...]][-ws|--workspaces][--include-workspace-root]
npm install
Install a package.
Usage:
npm install [<@scope>/]<pkg>npm install [<@scope>/]<pkg>@<tag>npm install [<@scope>/]<pkg>@<version>npm install [<@scope>/]<pkg>@<version range>npm install <alias>@npm:<name>npm install <folder>npm install <tarball file>npm install <tarball url>npm install <git:// url>npm install <github username>/<github project>
Options:
[-S|--save|--no-save|--save-prod|--save-dev|--save-optional|--save-peer][-E|--save-exact][-g|--global][--global-style][--legacy-bundling][--strict-peer-deps][--no-package-lock][--omit <dev|optional|peer> [--omit <dev|optional|peer> ...]][--ignore-scripts][--no-audit][--no-bin-links][--no-fund][--dry-run][-w|--workspace <workspace-name> [-w|--workspace <workspace-name> ...]][-ws|--workspaces][--include-workspace-root]
npm install-ci-test
Install a project with a clean slate and run tests.
Usage:
npm install-ci-test
Options:
[--no-audit][--ignore-scripts][--script-shell <script-shell>]
npm install-test
Install package(s) and run tests.
Usage:
npm install-test [<@scope>/]<pkg>npm install-test [<@scope>/]<pkg>@<tag>npm install-test [<@scope>/]<pkg>@<version>npm install-test [<@scope>/]<pkg>@<version range>npm install-test <alias>@npm:<name>npm install-test <folder>npm install-test <tarball file>npm install-test <tarball url>npm install-test <git:// url>npm install-test <github username>/<github project>
Options:
[-S|--save|--no-save|--save-prod|--save-dev|--save-optional|--save-peer][-E|--save-exact][-g|--global][--global-style][--legacy-bundling][--strict-peer-deps][--no-package-lock][--omit <dev|optional|peer> [--omit <dev|optional|peer> ...]][--ignore-scripts][--no-audit][--no-bin-links][--no-fund][--dry-run][-w|--workspace <workspace-name> [-w|--workspace <workspace-name> ...]][-ws|--workspaces][--include-workspace-root]
npm link
Symlink a package folder.
Usage:
npm link (in package dir)npm link [<@scope>/]<pkg>[@<version>]
Options:
[-S|--save|--no-save|--save-prod|--save-dev|--save-optional|--save-peer][-E|--save-exact][-g|--global][--global-style][--legacy-bundling][--strict-peer-deps][--no-package-lock][--omit <dev|optional|peer> [--omit <dev|optional|peer> ...]][--ignore-scripts][--no-audit][--no-bin-links][--no-fund][--dry-run][-w|--workspace <workspace-name> [-w|--workspace <workspace-name> ...]][-ws|--workspaces][--include-workspace-root]
npm login
Add a registry user account.
Usage:
npm login
Options:
[--registry <registry>][--scope <@scope>]
aliases:
adduseradd-user
TIP
npm login 该命令与 npm adduser 执行一致。
npm logout
Log out of the registry.
Usage:
npm logout
Options:
[--registry <registry>][--scope <@scope>]
npm ls
List installed packages.
Usage:
npm ls [[<@scope>/]<pkg> ...]
Options:
[-a|--all][--json][-l|--long][-p|--parseable][-g|--global][--depth <depth>][--omit <dev|optional|peer> [--omit <dev|optional|peer> ...]][--link][--package-lock-only][--no-unicode][-w|--workspace <workspace-name> [-w|--workspace <workspace-name> ...]][-ws|--workspaces][--include-workspace-root]
npm org
Manage orgs.
Usage:
npm org set orgname username [developer | admin | owner]npm org rm orgname usernamenpm org ls orgname [<username>]
Options:
[--registry <registry>][--otp <otp>][--json][-p|--parseable]
npm outdated
Check for outdated packages.
Usage:
npm outdated [[<@scope>/]<pkg> ...]
Options:
[-a|--all][--json][-l|--long][-p|--parseable][-g|--global][-w|--workspace <workspace-name> [-w|--workspace <workspace-name> ...]]
npm owner
Manage package owners.
Usage:
npm owner add <user> [<@scope>/]<pkg>npm owner rm <user> [<@scope>/]<pkg>npm owner ls [<@scope>/]<pkg>
Options:
[--registry <registry>][--otp <otp>]
npm pack
Create a tarball from a package.
Usage:
npm pack [[<@scope>/]<pkg>...]
Options:
[--dry-run][--json][--pack-destination <pack-destination>][-w|--workspace <workspace-name> [-w|--workspace <workspace-name> ...]][-ws|--workspaces][--include-workspace-root]
npm ping
Ping npm registry.
Usage:
npm ping
Options:
[--registry <registry>]
npm pkg
Manages your package.json.
Usage:
npm pkg set <key>=<value> [<key>=<value> ...]npm pkg get [<key> [<key> ...]]npm pkg delete <key> [<key> ...]
Options:
[-f|--force][--json][-w|--workspace <workspace-name> [-w|--workspace <workspace-name> ...]][-ws|--workspaces]
npm prefix
Display prefix.
Usage:
npm prefix [-g]
Options:
[-g|--global]
npm profile
Change settings on your registry profile.
Usage:
npm profile enable-2fa [auth-only|auth-and-writes]npm profile disable-2fanpm profile get [<key>]npm profile set <key> <value>
Options:
[--registry <registry>][--json][-p|--parseable][--otp <otp>]
npm prune
Remove extraneous packages.
Usage:
npm prune [[<@scope>/]<pkg>...]
Options:
[--omit <dev|optional|peer> [--omit <dev|optional|peer> ...]][--dry-run][--json][-w|--workspace <workspace-name> [-w|--workspace <workspace-name> ...]][-ws|--workspaces][--include-workspace-root]
npm publish
Publish a package.
Usage:
npm publish [<folder>]
Options:
[--tag <tag>][--access <restricted|public>][--dry-run][--otp <otp>][-w|--workspace <workspace-name> [-w|--workspace <workspace-name> ...]][-ws|--workspaces][--include-workspace-root]
npm query
The npm query command allows for usage of css selectors in order to retrieve an array of dependency objects.
Usage:
npm query <selector>
npm rebuild
Rebuild a package.
Usage:
npm rebuild [[<@scope>/]<name>[@<version>] ...]
Options:
[-g|--global][--no-bin-links][--ignore-scripts][-w|--workspace <workspace-name> [-w|--workspace <workspace-name> ...]][-ws|--workspaces][--include-workspace-root]
npm repo
Open package repository page in the browser.
Usage:
npm repo [<pkgname> [<pkgname> ...]]
Options:
[--no-browser|--browser <browser>][-w|--workspace <workspace-name> [-w|--workspace <workspace-name> ...]][-ws|--workspaces][--include-workspace-root]
npm restart
Restart a package.
Usage:
npm restart [-- <args>]
Options:
[--ignore-scripts][--script-shell <script-shell>]
npm root
Display npm root
Usage:
npm root
Options:
[-g|--global]
npm run-script
Run arbitrary package scripts.
Usage:
npm run-script <command> [-- <args>]
Options:
[-w|--workspace <workspace-name> [-w|--workspace <workspace-name> ...]][-ws|--workspaces][--include-workspace-root][--if-present][--ignore-scripts][--script-shell <script-shell>]
npm search
Search for packages.
Usage:
npm search [search terms ...]
Options:
[-l|--long][--json][--color|--no-color|--color always][-p|--parseable][--no-description][--searchopts <searchopts>][--searchexclude <searchexclude>][--registry <registry>][--prefer-online][--prefer-offline][--offline]
npm shrinkwrap
Lock down dependency versions for publication.
Usage:
npm shrinkwrap
npm star
Mark your favorite packages.
Usage:
npm star [<pkg>...]
Options:
[--registry <registry>][--no-unicode]
npm stars
View packages marked as favorites.
Usage:
npm stars [<user>]
Options:
[--registry <registry>]
npm start
Start a package.
Usage:
npm start [-- <args>]
Options:
[--ignore-scripts][--script-shell <script-shell>]
npm stop
Stop a package.
Usage:
npm stop [-- <args>]
Options:
[--ignore-scripts][--script-shell <script-shell>]
npm team
Manage organization teams and team memberships.
Usage:
npm team create <scope:team> [--otp <otpcode>]npm team destroy <scope:team> [--otp <otpcode>]npm team add <scope:team> <user> [--otp <otpcode>]npm team rm <scope:team> <user> [--otp <otpcode>]npm team ls <scope>|<scope:team>
Options:
[--registry <registry>][--otp <otp>][-p|--parseable] [--json]
npm test
Test a package.
Usage:
npm test [-- <args>]
Options:
[--ignore-scripts][--script-shell <script-shell>]
npm token
Manage your authentication tokens.
Usage:
npm token listnpm token revoke <id|token>npm token create [--read-only] [--cidr=list]
Options:
[--read-only][--cidr <cidr> [--cidr <cidr> ...]][--registry <registry>][--otp <otp>]
npm uninstall
Remove a package.
Usage:
npm uninstall [<@scope>/]<pkg>...
Options:
[-S|--save|--no-save|--save-prod|--save-dev|--save-optional|--save-peer][-w|--workspace <workspace-name> [-w|--workspace <workspace-name> ...]][-ws|--workspaces][--include-workspace-root]
npm unpublish
Remove a package from the registry.
Usage:
npm unpublish [<@scope>/]<pkg>[@<version>]
Options:
[--dry-run][-f|--force][-w|--workspace <workspace-name> [-w|--workspace <workspace-name> ...]][-ws|--workspaces]
npm update
Update packages.
Usage:
npm update [<pkg>...]
Options:
[-g|--global][--global-style][--legacy-bundling][--strict-peer-deps][--no-package-lock][--omit <dev|optional|peer> [--omit <dev|optional|peer> ...]][--ignore-scripts][--no-audit][--no-bin-links][--no-fund][--dry-run][-w|--workspace <workspace-name> [-w|--workspace <workspace-name> ...]][-ws|--workspaces][--include-workspace-root]
npm version
Bump a package version.
Usage:
npm version [<newversion> | major | minor | patch | premajor | preminor | prepatch | prerelease | from-git]
Options:
[--allow-same-version][--no-commit-hooks][--no-git-tag-version][--json][--preid prerelease-id][--sign-git-tag][-w|--workspace <workspace-name> [-w|--workspace <workspace-name> ...]][-ws|--workspaces][--include-workspace-root]
npm view
View registry info.
Usage:
npm view [<@scope>/]<pkg>[@<version>] [<field>[.subfield]...]
Options:
[--json][-w|--workspace <workspace-name> [-w|--workspace <workspace-name> ...]][-ws|--workspaces][--include-workspace-root]
npm whoami
Display npm username.
Usage:
npm whoami
Options:
[--registry <registry>]
npx
Run a command from a local or remote npm package.
Usage:
npm exec -- <pkg>[@<version>] [args...]npm exec --package=<pkg>[@<version>] -- <cmd> [args...]npm exec -c '<cmd> [args...]'npm exec --package=foo -c '<cmd> [args...]'
Options:
[--package <pkg>[@<version>][--package <pkg>[@<version>] ...]][-c|--call <call>][-w|--workspace <workspace-name> [-w|--workspace <workspace-name> ...]][-ws|--workspaces] [--include-workspace-root]
TIP
npx 命令与 npm exec 命令执行一致。
实际上,npx 是 npm exec 的别名。