Skip to content

本节总结了 npm 命令行工具的使用方法。

该节 Commands 是基于npm@9.6.6

npm access

Set access level on published packages.

Usage:

  1. npm access public [<package>]
  2. npm access restricted [<package>]
  3. npm access grant <read-only|read-write> <scope:team> [<package>]
  4. npm access revoke <scope:team> [<package>]
  5. npm access 2fa-required [<package>]
  6. npm access 2fa-not-required [<package>]
  7. npm access ls-packages [<user>|<scope>|<scope:team>]
  8. npm access ls-collaborators [<package> [<user>]]
  9. npm access edit [<package>]

Options:

  • [--registry <registry>]
  • [--otp <otp>]

npm adduser

Add a registry user account.

Usage:

  1. npm adduser

Options:

  1. [--registry <registry>]
  2. [--scope <@scope>]

npm audit

Run a security audit.

Usage:

  1. npm audit 进行安全漏洞检测
  2. 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:

  1. npm bugs [<pkgname>]

Options:

  • [--no-browser|--browser <browser>]
  • [--registry <registry>]

npm cache

Manipulates packages cache.

Usage:

  1. npm cache add <tarball file>
  2. npm cache add <folder>
  3. npm cache add <tarball url>
  4. npm cache add <git url>
  5. npm cache add <name>@<version>
  6. npm cache clean [<key>]
  7. npm cache ls [<name>@<version>]
  8. npm cache verify

Options:

  • [--cache <cache>]

npm ci

Install a project with a clean slate.

Usage:

  1. npm ci

Options:

  • [--no-audit]
  • [--ignore-scripts]
  • [--script-shell <script-shell>]

npm completion

Tab Completion for npm.

Usage:

  1. npm completion

npm config

Manage the npm configuration files.

Usage:

  1. npm config set <key>=<value> [<key>=<value> ...]
  2. npm config get [<key> [<key> ...]]
  3. npm config delete <key> [<key> ...]
  4. npm config list [--json]
  5. 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:

  1. 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:

  1. npm deprecate <pkg>[@<version>] <message>

Options:

  • [--registry <registry>]
  • [--otp <otp>]

npm diff

The registry diff command.

Usage:

  1. 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:

  1. npm dist-tag add <pkg>@<version> [<tag>]
  2. npm dist-tag rm <pkg> <tag>
  3. 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:

  1. 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:

  1. npm doctor

Options:

  • [--registry <registry>]

npm edit

Edit an installed package.

Usage:

  1. npm edit <pkg>[/<subpkg>...]

Options:

  • [--editor <editor>]

npm exec

Run a command from a local or remote npm package.

Usage:

  1. npm exec -- <pkg>[@<version>] [args...]
  2. npm exec --package=<pkg>[@<version>] -- <cmd> [args...]
  3. npm exec -c '<cmd> [args...]'
  4. 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:

  1. npm explain <folder | specifier>

Options:

  • [--json]
  • [-w|--workspace <workspace-name> [-w|--workspace <workspace-name> ...]]

npm explore

Browse an installed package.

Usage:

  1. npm explore <pkg> [ -- <command>]

Options:

  • [--shell <shell>]

npm find-dupes

Find duplication in the package tree.

Usage:

  1. 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:

  1. 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:

  1. npm help <term> [<terms..>]

Options:

  • [--viewer <viewer>]

Search npm help documentation.

Usage:

  1. npm help-search <text>

Options:

  • [-l|--long]

npm hook

Manage registry hooks.

Usage:

  1. npm hook add <pkg> <url> <secret> [--type=<type>]
  2. npm hook ls [pkg]
  3. npm hook rm <id>
  4. npm hook update <id> <url> <secret>

Options:

  • [--registry <registry>] [--otp <otp>]

npm init

Create a package.json file.

Usage:

  1. npm init [--force|-f|--yes|-y|--scope]
  2. npm init <@scope> (same as npx <@scope>/create)
  3. 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:

  1. npm install [<@scope>/]<pkg>
  2. npm install [<@scope>/]<pkg>@<tag>
  3. npm install [<@scope>/]<pkg>@<version>
  4. npm install [<@scope>/]<pkg>@<version range>
  5. npm install <alias>@npm:<name>
  6. npm install <folder>
  7. npm install <tarball file>
  8. npm install <tarball url>
  9. npm install <git:// url>
  10. 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:

  1. npm install-ci-test

Options:

  • [--no-audit]
  • [--ignore-scripts]
  • [--script-shell <script-shell>]

npm install-test

Install package(s) and run tests.

Usage:

  1. npm install-test [<@scope>/]<pkg>
  2. npm install-test [<@scope>/]<pkg>@<tag>
  3. npm install-test [<@scope>/]<pkg>@<version>
  4. npm install-test [<@scope>/]<pkg>@<version range>
  5. npm install-test <alias>@npm:<name>
  6. npm install-test <folder>
  7. npm install-test <tarball file>
  8. npm install-test <tarball url>
  9. npm install-test <git:// url>
  10. 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]

Symlink a package folder.

Usage:

  1. npm link (in package dir)
  2. 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:

  1. npm login

Options:

  • [--registry <registry>]
  • [--scope <@scope>]

aliases:

  • adduser
  • add-user

TIP

npm login 该命令与 npm adduser 执行一致。

npm logout

Log out of the registry.

Usage:

  1. npm logout

Options:

  • [--registry <registry>]
  • [--scope <@scope>]

npm ls

List installed packages.

Usage:

  1. 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:

  1. npm org set orgname username [developer | admin | owner]
  2. npm org rm orgname username
  3. npm org ls orgname [<username>]

Options:

  • [--registry <registry>]
  • [--otp <otp>]
  • [--json]
  • [-p|--parseable]

npm outdated

Check for outdated packages.

Usage:

  1. 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:

  1. npm owner add <user> [<@scope>/]<pkg>
  2. npm owner rm <user> [<@scope>/]<pkg>
  3. npm owner ls [<@scope>/]<pkg>

Options:

  • [--registry <registry>]
  • [--otp <otp>]

npm pack

Create a tarball from a package.

Usage:

  1. 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:

  1. npm ping

Options:

  • [--registry <registry>]

npm pkg

Manages your package.json.

Usage:

  1. npm pkg set <key>=<value> [<key>=<value> ...]
  2. npm pkg get [<key> [<key> ...]]
  3. npm pkg delete <key> [<key> ...]

Options:

  • [-f|--force]
  • [--json]
  • [-w|--workspace <workspace-name> [-w|--workspace <workspace-name> ...]]
  • [-ws|--workspaces]

npm prefix

Display prefix.

Usage:

  1. npm prefix [-g]

Options:

  • [-g|--global]

npm profile

Change settings on your registry profile.

Usage:

  1. npm profile enable-2fa [auth-only|auth-and-writes]
  2. npm profile disable-2fa
  3. npm profile get [<key>]
  4. npm profile set <key> <value>

Options:

  • [--registry <registry>]
  • [--json]
  • [-p|--parseable]
  • [--otp <otp>]

npm prune

Remove extraneous packages.

Usage:

  1. 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:

  1. 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:

  1. npm query <selector>

npm rebuild

Rebuild a package.

Usage:

  1. 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:

  1. 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:

  1. npm restart [-- <args>]

Options:

  • [--ignore-scripts]
  • [--script-shell <script-shell>]

npm root

Display npm root

Usage:

  1. npm root

Options:

  • [-g|--global]

npm run-script

Run arbitrary package scripts.

Usage:

  1. 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>]

Search for packages.

Usage:

  1. 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:

  1. npm shrinkwrap

npm star

Mark your favorite packages.

Usage:

  1. npm star [<pkg>...]

Options:

  • [--registry <registry>]
  • [--no-unicode]

npm stars

View packages marked as favorites.

Usage:

  1. npm stars [<user>]

Options:

  • [--registry <registry>]

npm start

Start a package.

Usage:

  1. npm start [-- <args>]

Options:

  • [--ignore-scripts]
  • [--script-shell <script-shell>]

npm stop

Stop a package.

Usage:

  1. npm stop [-- <args>]

Options:

  • [--ignore-scripts]
  • [--script-shell <script-shell>]

npm team

Manage organization teams and team memberships.

Usage:

  1. npm team create <scope:team> [--otp <otpcode>]
  2. npm team destroy <scope:team> [--otp <otpcode>]
  3. npm team add <scope:team> <user> [--otp <otpcode>]
  4. npm team rm <scope:team> <user> [--otp <otpcode>]
  5. npm team ls <scope>|<scope:team>

Options:

  • [--registry <registry>]
  • [--otp <otp>]
  • [-p|--parseable] [--json]

npm test

Test a package.

Usage:

  1. npm test [-- <args>]

Options:

  • [--ignore-scripts]
  • [--script-shell <script-shell>]

npm token

Manage your authentication tokens.

Usage:

  1. npm token list
  2. npm token revoke <id|token>
  3. npm token create [--read-only] [--cidr=list]

Options:

  • [--read-only]
  • [--cidr <cidr> [--cidr <cidr> ...]]
  • [--registry <registry>]
  • [--otp <otp>]

npm uninstall

Remove a package.

Usage:

  1. 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:

  1. 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:

  1. 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:

  1. 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:

  1. 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:

  1. npm whoami

Options:

  • [--registry <registry>]

npx

Run a command from a local or remote npm package.

Usage:

  1. npm exec -- <pkg>[@<version>] [args...]
  2. npm exec --package=<pkg>[@<version>] -- <cmd> [args...]
  3. npm exec -c '<cmd> [args...]'
  4. 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 命令执行一致。

实际上,npxnpm exec 的别名。