选择 CLI 版本:



npm rebuild [<package-spec>] ...]
alias: rb




This command does the following:

  1. 执行生命周期脚本(preinstallinstallpostinstallprepare

    Execute lifecycle scripts (preinstall, install, postinstall, prepare)

  2. 根据 bin 链接是否启用来链接 bin

    Links bins depending on whether bin links are enabled


This command is particularly useful in scenarios including but not limited to:

  1. 安装新版本的 node.js,你需要使用更新的二进制文件重新编译所有 C++ 附加组件。

    Installing a new version of node.js, where you need to recompile all your C++ add-ons with the updated binary.

  2. 使用 --ignore-scripts--no-bin-links 进行安装,以明确选择要构建和/或链接 bin 的包。

    Installing with --ignore-scripts and --no-bin-links, to explicitly choose which packages to build and/or link bins.


If one or more package specs are provided, then only packages with a name and version matching one of the specifiers will be rebuilt.

通常,你不需要运行 npm rebuild,因为它已经作为 npm install 的一部分为你完成(除非你使用 --ignore-scripts--no-bin-links 抑制了这些步骤)。

Usually, you should not need to run npm rebuild as it is already done for you as part of npm install (unless you suppressed these steps with --ignore-scripts or --no-bin-links).

如果包的根目录中有 binding.gyp 文件,则 npm 将使用默认安装钩子:

If there is a binding.gyp file in the root of your package, then npm will use a default install hook:

"scripts": {
"install": "node-gyp rebuild"

如果 package.json 有自己的 installpreinstall 脚本,则此默认行为将被抑制。如果包指定 "gypfile": false,它也会被抑制

This default behavior is suppressed if the package.json has its own install or preinstall scripts. It is also suppressed if the package specifies "gypfile": false




  • 默认值:false

    Default: false

  • 类型:布尔值

    Type: Boolean

在 "global" 模式下运行,以便将包安装到 prefix 文件夹而不是当前工作目录。有关行为差异的更多信息,请参见 文件夹

Operates in "global" mode, so that packages are installed into the prefix folder instead of the current working directory. See folders for more on the differences in behavior.

  • 包安装到 {prefix}/lib/node_modules 文件夹,而不是当前工作目录。

    packages are installed into the {prefix}/lib/node_modules folder, instead of the current working directory.

  • bin 文件链接到 {prefix}/bin

    bin files are linked to {prefix}/bin

  • 手册页链接到 {prefix}/share/man

    man pages are linked to {prefix}/share/man

  • 默认值:true

    Default: true

  • 类型:布尔值

    Type: Boolean

告诉 npm 为包的可执行文件创建符号链接(或 Windows 上的 .cmd 垫片)。

Tells npm to create symlinks (or .cmd shims on Windows) for package executables.

设置为 false 使其不执行此操作。这可以用来解决某些文件系统不支持符号链接的事实,即使在表面上是 Unix 系统上也是如此。

Set to false to have it not do this. This can be used to work around the fact that some file systems don't support symlinks, even on ostensibly Unix systems.


  • 默认值:false

    Default: false

  • 类型:布尔值

    Type: Boolean

在前台进程中运行已安装包的所有构建脚本(即 preinstallinstallpostinstall)脚本,与主 npm 进程共享标准输入、输出和错误。

Run all build scripts (ie, preinstall, install, and postinstall) scripts for installed packages in the foreground process, sharing standard input, output, and error with the main npm process.


Note that this will generally make installs run slower, and be much noisier, but can be useful for debugging.


  • 默认值:false

    Default: false

  • 类型:布尔值

    Type: Boolean

如果为 true,npm 不会运行 package.json 文件中指定的脚本。

If true, npm does not run scripts specified in package.json files.

请注意,如果设置了 ignore-scripts,则明确旨在运行特定脚本的命令(例如 npm startnpm stopnpm restartnpm testnpm run-script)仍将运行其预期的脚本,但它们不会运行任何前置或后置脚本。

Note that commands explicitly intended to run a particular script, such as npm start, npm stop, npm restart, npm test, and npm run-script will still run their intended script if ignore-scripts is set, but they will not run any pre- or post-scripts.


  • 默认值:


  • 类型:字符串(可以设置多次)

    Type: String (can be set multiple times)


Enable running a command in the context of the configured workspaces of the current project while filtering by running only the workspaces defined by this configuration option.

workspace 配置的有效值为:

Valid values for the workspace config are either:

  • 工作区名称

    Workspace names

  • 工作区目录的路径

    Path to a workspace directory

  • 父工作区目录的路径(将导致选择该文件夹中的所有工作区)

    Path to a parent workspace directory (will result in selecting all workspaces within that folder)

npm init 命令设置时,可以将其设置为尚不存在的工作区的文件夹,以创建文件夹并将其设置为项目中的全新工作区。

When set for the npm init command, this may be set to the folder of a workspace which does not yet exist, to create the folder and set it up as a brand new workspace within the project.


This value is not exported to the environment for child processes.


  • 默认值:null

    Default: null

  • 类型:空值或布尔值

    Type: null or Boolean

设置为 true 可在所有已配置工作区的上下文中运行该命令。

Set to true to run the command in the context of all configured workspaces.

显式将此设置为 false 将导致像 install 这样的命令完全忽略工作区。未明确设置时:

Explicitly setting this to false will cause commands like install to ignore workspaces altogether. When not set explicitly:

  • node_modules 树上运行的命令(安装、更新等)会将工作区链接到 node_modules 文件夹。* 执行其他操作(测试、执行、发布等)的命令将在根项目上运行,除非在 workspace 配置中指定了一个或多个工作区。

    Commands that operate on the node_modules tree (install, update, etc.) will link workspaces into the node_modules folder. - Commands that do other things (test, exec, publish, etc.) will operate on the root project, unless one or more workspaces are specified in the workspace config.


This value is not exported to the environment for child processes.


  • 默认值:false

    Default: false

  • 类型:布尔值

    Type: Boolean


Include the workspace root when workspaces are enabled for a command.

当为 false 时,通过 workspace 配置指定单个工作区,或通过 workspaces 标志指定所有工作区,将导致 npm 仅在指定的工作区上运行,而不是在根项目上运行。

When false, specifying individual workspaces via the workspace config, or all workspaces via the workspaces flag, will cause npm to operate only on the specified workspaces, and not on the root project.


This value is not exported to the environment for child processes.

  • 默认值:false

    Default: false

  • 类型:布尔值

    Type: Boolean


When set file: protocol dependencies will be packed and installed as regular dependencies instead of creating a symlink. This option has no effect on workspaces.


See Also

npm 中文网 - 粤ICP备13048890号