node.js npm install 和 npm run build 有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43664200/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the difference between npm install and npm run build?
提问by BalajiK
What is the difference between npm installand npm run build?
npm install和 和有npm run build什么区别?
I have noticed in my project that sometimes npm starts failing when npm installis performed, but, upon running npm run build, it works just fine.
我在我的项目中注意到,有时 npm 在npm install执行时开始失败,但是,在运行时npm run build,它工作得很好。
How does the inner workings of these two targets namely installand run builddiffer?
如何这两个目标的内部运作,即install与run build不同?
采纳答案by Churro
npm installinstalls dependencies into the node_modules/directory, for the node project you're working on. You can call installon another node.js project (module), to install it as a dependency for your project.
npm install将依赖项安装到node_modules/目录中,用于您正在处理的节点项目。您可以调用install另一个 node.js 项目(模块),将其安装为您项目的依赖项。
npm run buildis an alias for npm build, and it does nothing unless you specify what "build" does in your package.json file. It lets you perform any necessary building/prep tasks for your project, prior to it being used in another project.
npm run build是 的别名npm build,除非您在 package.json 文件中指定“build”做什么,否则它什么都不做。它允许您在项目用于另一个项目之前为项目执行任何必要的构建/准备任务。
buildis called by linkand installcommands, according to the documentation for build:
build根据build 的文档,由link和install命令调用:
This is the plumbing command called by npm link and npm install.
这是由 npm link 和 npm install 调用的管道命令。
回答by CTS_AE
NPM in 2019
2019年NPM
npm buildno longer exists. You must call npm run buildnow. More info below.
npm build不复存在。你npm run build现在必须打电话。更多信息如下。
TLDR;
TLDR;
npm install: installs dependencies, then calls the installfrom the package.jsonscriptsfield.
npm install: 安装依赖项,然后install从package.jsonscripts字段调用。
npm run build: runs the build field from the package.jsonscriptsfield.
npm run build: 从package.jsonscripts现场运行构建字段。
NPM Scripts Field
NPM 脚本字段
https://docs.npmjs.com/misc/scripts
https://docs.npmjs.com/misc/scripts
There are many things you can put into the npm package.jsonscripts field. Check out the documentation link above more above the lifecycle of the scripts - most have pre and post hooks that you can run scripts before/after install, publish, uninstall, test, start, stop, shrinkwrap, version.
您可以在 npmpackage.json脚本字段中放入很多东西。在脚本的生命周期之上查看上面的文档链接 - 大多数都有 pre 和 post 挂钩,您可以在安装、发布、卸载、测试、启动、停止、收缩包装、版本之前/之后运行脚本。
To Complicate Things
把事情复杂化
npm installis not the same asnpm run installnpm installinstallspackage.jsondependencies, then runs thepackage.jsonscripts.install- (Essentially calls
npm run installafter dependencies are installed.
- (Essentially calls
npm run installonly runs thepackage.jsonscripts.install, it will not install dependencies.npm buildused to be a valid command (used to be the same asnpm 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
npm install不一样npm run installnpm install安装package.json依赖项,然后运行package.jsonscripts.install- (本质上
npm run install是在安装依赖之后调用。
- (本质上
npm run install只运行package.jsonscripts.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
回答by MKP
The main difference is ::
npm installis a npm cli-command which does the predefined thing i.e, as written by Churro, to install dependencies specified inside package.json
npm runcommand-nameor npm run-scriptcommand-name( ex.npm run build) 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 buildis 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).
主要区别是::
npm install是一个 npm cli 命令,它执行预定义的事情,即由 Churro 编写,以安装 package.json 中指定的依赖项
npm run command-name或npm run-script command-name(例如npm run build)也是一个预定义的 cli 命令,用于运行您的自定义脚本,使用指定的名称代替“command-name”。因此,在这种情况下,npm run build是一个名为“build”的自定义脚本命令,它将执行其中指定的任何内容(例如,在下面的示例 package.json 中给出的echo 'hello world')。
Ponits to note::
注意事项:
1) One more thing, npm buildand npm run buildare two different things npm buildwill do as written by Churro, but npm run buildwill do custom work written inside package.json
1)还有一两件事,npm build而且npm run build是两个不同的东西npm build会做如西班牙油条写的,但npm run build会做定制工作里面写package.json
2) And npm buildand npm run buildare not same. What I means is, you cannot specify some thing inside custom build (npm run build) script and expect npm buildto do the same. Try following thing to verify in your package.json:
2) andnpm build和npm run build不一样。我的意思是,您不能在自定义构建 ( 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": {}
}
and run npm run buildand npm buildone by one and you will see the difference. For more about commands kindly follow npm documentation.
并一一运行npm run build,npm build您将看到不同之处。有关命令的更多信息,请遵循npm 文档。
Cheers!!
干杯!!
回答by Chanoch
npm installinstalls the depedendencies in your package.json config.npm run buildruns the script "build" and created a script which runs your application - let's say server.jsnpm startruns the "start" script which will then be "node server.js"
npm install在 package.json 配置中安装依赖项。npm run build运行脚本“build”并创建一个运行您的应用程序的脚本 - 假设 server.jsnpm start运行“start”脚本,然后将是“node server.js”
It's difficult to tell exactly what the issue was but basically if you look at your scripts configuration, I would guess that "build" uses some kind of build tool to create your application while "start" assumes the build has been done but then fails if the file is not there.
很难确切地说出问题是什么,但基本上如果你查看你的脚本配置,我猜“构建”使用某种构建工具来创建你的应用程序,而“开始”假设构建已经完成但如果失败该文件不存在。
You are probably using bower or grunt - I seem to remember that a typical grunt application will have defined those scripts as well as a "clean" script to delete the last build.
您可能正在使用 bower 或 grunt - 我似乎记得典型的 grunt 应用程序将定义这些脚本以及删除最后一个构建的“干净”脚本。
Build tools tend to create a file in a bin/, dist/, or build/ folder which the start script then calls - e.g. "node build/server.js". When your npm startfails, it is probably because you called npm cleanor similar to delete the latest build so your application file is not present causing npm start to fail.
构建工具倾向于在启动脚本调用的 bin/、dist/ 或 build/ 文件夹中创建一个文件 - 例如“node build/server.js”。当您npm start失败时,可能是因为您调用npm clean或类似方法删除了最新版本,因此您的应用程序文件不存在导致 npm start 失败。
npm build's source code - to touch on the discussion in this question - is in github for you to have a look at if you like. If you run npm builddirectly and you have a "build" script defined, it will exit with an error asking you to call your build script as npm run-script buildso it's not the same as npm run script.
npm build 的源代码 - 涉及这个问题中的讨论 - 在 github 中,如果您愿意,可以查看。如果您npm build直接运行并且定义了“构建”脚本,它将退出并显示错误,要求您调用构建脚本,npm run-script build因为它与npm run script.
I'm not quite sure what npm builddoes, but it seems to be related to postinstall and packaging scripts in dependencies. I assume that this might be making sure that any CLI build scripts's or native libraries required by dependencies are built for the specific environment after downloading the package. This will be why link and install call this script.
我不太确定是什么npm build,但它似乎与依赖项中的 postinstall 和打包脚本有关。我认为这可能是为了确保在下载包后为特定环境构建依赖项所需的任何 CLI 构建脚本或本机库。这就是链接和安装调用此脚本的原因。

