npm install和npm run build有什么区别?

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

这两个目标(即安装和运行构建)的内部工作原理有何不同?


当前回答

2019年的NPM

NPM构建不再存在。你现在必须调用npm run build。更多信息如下。

TLDR;

NPM install:安装依赖项,然后从包中调用install。Json脚本字段。

NPM run build:从包中运行build字段。Json脚本字段。


NPM脚本字段

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

你可以把很多东西放入npm包中。Json脚本字段。查看上面的文档链接,更多的是脚本的生命周期-大多数都有前后钩子,您可以在安装,发布,卸载,测试,启动,停止,收缩包装,版本之前/之后运行脚本。


把事情复杂化

npm install is not the same as npm run install npm install installs package.json dependencies, then runs the package.json scripts.install (Essentially calls npm run install after dependencies are installed. npm run install only runs the package.json scripts.install, it will not install dependencies. npm build used to be a valid command (used to be the same as npm run build) but it no longer is; it is now an internal command. If you run it you'll get: npm WARN build npm build called with no arguments. Did you mean to npm run-script build? You can read more on the documentation: https://docs.npmjs.com/cli/build or https://docs.npmjs.com/cli/v6/commands/npm-build


额外的笔记

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

NPM start和NPM run start是一样的 NPM test ==> NPM运行测试

其他回答

NPM install在你的包中安装依赖项。json配置。 NPM run build运行“build”脚本,并创建一个运行应用程序的脚本——比如server.js NPM start运行“start”脚本,然后是“node server.js”

很难确切地说问题是什么,但基本上如果你看看你的脚本配置,我猜“build”使用某种构建工具来创建你的应用程序,而“start”假设构建已经完成,但如果文件不存在,就会失败。

您可能正在使用bower或grunt -我似乎记得一个典型的grunt应用程序将定义这些脚本以及一个“干净”脚本来删除最后的构建。

构建工具倾向于在bin/, dist/或Build /文件夹中创建一个文件,然后开始脚本调用该文件。“节点构建/ server.js”。当你的npm启动失败时,很可能是因为你调用了npm clean或类似于删除最新的构建,所以你的应用程序文件不存在,导致npm启动失败。

NPM构建的源代码-涉及到这个问题的讨论-在github中,如果你喜欢可以看看。如果你直接运行npm build,并且你已经定义了一个“build”脚本,它将退出并提示你将构建脚本调用为npm run-script build,因此它与npm run script不同。

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

主要区别是:

npm install是一个npm cli命令,它做预定义的事情,即,由Churro编写,安装package.json中指定的依赖项。

npm run %command-name%或npm run-script %command-name%也是一个预定义的cli命令,用于运行你的自定义脚本,使用指定的名称代替“command-name”。因此,在这种情况下,npm run build是一个名为“build”的自定义脚本命令,并将执行其中指定的任何操作(例如在下面的package.json示例中给出的echo 'hello world')。

注意事项:

还有一件事,npm build和npm run build是两个不同的东西,npm run build会做写在包里的自定义工作。Json和NPM构建是一个预定义的脚本(不能直接使用)。 你不能在自定义构建脚本(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运行构建和NPM构建,你会看到区别。有关命令的更多信息,请参考npm文档。

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

NPM运行build不做任何事情,除非你在你的包中指定“build”做什么。json文件。它可以让您执行任何必要的构建/准备任务,为您的项目,在它被用于另一个项目之前。

NPM build是一个内部命令,由link和install命令调用,根据build的文档:

这是由npm link和npm install调用的管道命令。

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

2019年的NPM

NPM构建不再存在。你现在必须调用npm run build。更多信息如下。

TLDR;

NPM install:安装依赖项,然后从包中调用install。Json脚本字段。

NPM run build:从包中运行build字段。Json脚本字段。


NPM脚本字段

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

你可以把很多东西放入npm包中。Json脚本字段。查看上面的文档链接,更多的是脚本的生命周期-大多数都有前后钩子,您可以在安装,发布,卸载,测试,启动,停止,收缩包装,版本之前/之后运行脚本。


把事情复杂化

npm install is not the same as npm run install npm install installs package.json dependencies, then runs the package.json scripts.install (Essentially calls npm run install after dependencies are installed. npm run install only runs the package.json scripts.install, it will not install dependencies. npm build used to be a valid command (used to be the same as npm run build) but it no longer is; it is now an internal command. If you run it you'll get: npm WARN build npm build called with no arguments. Did you mean to npm run-script build? You can read more on the documentation: https://docs.npmjs.com/cli/build or https://docs.npmjs.com/cli/v6/commands/npm-build


额外的笔记

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

NPM start和NPM run start是一样的 NPM test ==> NPM运行测试