npm install 和 npm 运行 build 有什么区别?

What is the difference between npm install and npm run build?

npm installnpm run build有什么区别?

我在我的项目中注意到,当执行 npm install 时,有时 npm 开始失败,但是,在 运行 npm run build 时,它工作正常。

installrun build 这两个目标的内部工作原理有何不同?

npm install 将依赖项安装到 node_modules/ 目录中,用于您正在处理的节点项目。您可以在另一个 node.js 项目(模块)上调用 install,将其安装为项目的依赖项。

npm run build 什么都不做,除非您在 package.json 文件中指定“构建”的作用。它允许您在将项目用于另一个项目之前为项目执行任何必要的 building/prep 任务。

npm build是一个内部命令,被linkinstall命令调用,根据the documentation for build:

This is the plumbing command called by npm link and npm install.

您不会正常调用 npm build,因为它在内部用于使用 node-gyp 构建原生 C/C++ Node 插件。

主要区别是:

npm install is a npm CLI-command which does the predefined thing i.e., as written by Churro, to install dependencies specified inside package.json.

npm run %command-name% or npm run-script %command-name% is also a CLI-command predefined to run your custom scripts with the name specified in place of "command-name". So, in this case npm run build is a custom script command with the name "build" and will do anything specified inside it (for instance echo 'hello world' given in below example package.json).

注意事项::

  1. 还有一点,npm buildnpm run build是两个不同的东西,npm run build会做自定义工作package.jsonnpm build 中是一个 预定义的 脚本(不能直接使用)。

  2. 您不能在自定义构建脚本 (npm run build) 脚本中指定某些内容并期望 npm build 执行相同的操作。尝试在您的 package.json:

    中验证以下内容
{
    "name": "demo",
    "version": "1.0.0",
    "description": "",
    "main": "index.js",
    "scripts": {
        "build": "echo 'hello build'"
    },
    "keywords": [],
    "author": "",
    "license": "ISC",
    "devDependencies": {},
    "dependencies": {}
}

和 运行 npm run buildnpm build 一个接一个,您会看到不同之处。有关命令的更多信息,请关注 npm documentation.

  • npm install 在您的 package.json 配置中安装依赖项。
  • npm run build 运行 脚本 "build" 并创建了一个脚本,其中 运行 是您的应用程序 - 比方说 server.js
  • npm start 运行s "start" 脚本然后将是 "node server.js"

很难准确判断问题出在哪里,但基本上如果您查看脚本配置,我猜想 "build" 使用某种构建工具来创建您的应用程序,而 "start" 假设构建已经完成,但如果文件不存在则失败。

您可能正在使用 bower 或 g运行t - 我似乎记得典型的 g运行t 应用程序将定义这些脚本以及一个 "clean" 脚本来删除最后一个版本。

构建工具往往会在 bin/、dist/ 或 build/ 文件夹中创建一个文件,然后启动脚本会调用该文件 - 例如"node build/server.js"。当您的 npm start 失败时,可能是因为您调用了 npm clean 或类似的方法来删除最新版本,因此您的应用程序文件不存在导致 npm 启动失败。

npm build 的源代码——触及这个问题中的讨论——在 github 中,如果你愿意,可以看看。如果您直接 运行 npm build 并且定义了 "build" 脚本,它将退出并出现错误,要求您将构建脚本称为 npm run-script build 所以它与npm run script

我不太确定npm build 做了什么,但它似乎与依赖项中的安装后和打包脚本有关。我假设这可能是为了确保在下载包后为特定环境构建依赖项所需的任何 CLI 构建脚本或本机库。这就是 link 和安装调用此脚本的原因。

2019 年 NPM

npm build 已不存在。你现在必须打电话给 npm run build。更多信息如下。

TLDR;

npm install:安装依赖项,然后从 package.json scripts 字段调用 install

npm run build: 运行s 来自 package.json scripts 字段的构建字段。


NPM 脚本字段

https://docs.npmjs.com/misc/scripts

您可以在 npm package.json 脚本字段中放入很多内容。查看上面的文档link,了解更多关于脚本生命周期的内容 - 大多数都有预挂钩和 post 挂钩,您可以 运行 脚本 before/after 安装、发布、卸载、测试,开始、停止、收缩包装、版本。


使事情复杂化

  • npm installnpm run install
  • 不同
  • npm install 安装 package.json 依赖项,然后 运行 安装 package.json scripts.install
    • (本质上是在安装依赖项后调用npm run install
  • npm run install 只有 运行 package.json scripts.install,它 不会安装依赖项 .
  • npm build 曾经是一个有效的命令(曾经与 npm run build 相同)但现在不再是;它现在是一个内部命令。如果你 运行 它你会得到: npm WARN build npm build called with no arguments. Did you mean to npm run-script build? 你可以阅读更多文档: https://docs.npmjs.com/cli/build or https://docs.npmjs.com/cli/v6/commands/npm-build

附加说明

还有两个顶级命令将 运行 脚本,它们是:

  • npm start 等同于 npm run start
  • npm test ==> npm run test