Javascript `npm build` 不运行 package.json 中名为“build”的脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29939697/
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
`npm build` doesn't run the script named "build" in package.json
提问by mikemaccana
For a new module I'm trying to use npm buildwithout gulp / Grunt / other specialised build tools.
对于一个新模块,我试图在npm build没有 gulp / Grunt / 其他专门构建工具的情况下使用。
"scripts": {
"build": "node build.js"
},
My build.js is simply
我的 build.js 很简单
console.log('Hello')
However, running
然而,运行
npm build
Simply exits without printing anything, with a status of 0.
直接退出而不打印任何内容,状态为 0。
Running:
跑步:
npm install
Also does all the normal things, but does not run build.js either.
也做所有正常的事情,但也不运行 build.js。
How can I make npm run my build script?
如何让 npm 运行我的构建脚本?
Edit: even simple bash commands don't seem to work, eg
编辑:即使是简单的 bash 命令似乎也不起作用,例如
"scripts": {
"build": "touch TESTFILE"
},
Doesn't make a file with that name.
不制作具有该名称的文件。
回答by jayphelps
Unfortunately npm buildis already an internalcommand, as described in the docs:
不幸的npm build是已经是一个内部命令,如文档中所述:
This is the plumbing command called by npm link and npm install. It should generally not be called directly.
这是由 npm link 和 npm install 调用的管道命令。 一般不应直接调用。
Because that command already exists, it always shadows over your "build": "node build.js".
因为该命令已经存在,所以它总是隐藏在您的"build": "node build.js".
The fully-qualified way to run your own script is with run-scriptor its alias run:
运行您自己的脚本的完全限定方式是 withrun-script或其别名run:
$ npm run build
npm startand others are the short-hand way, but is only an option when an existing npm command doesn't shadow it, like npm builddoes.
npm start和其他人是速记方式,但仅当现有的 npm 命令不像它那样隐藏它时才作为一种选择npm build。
For posterity (as others have mentioned) npm buildis used by npm to build native C/C++ Node addons using node-gyp. It's not documented well because usually it happens automatically, but if you're interested the source code is here.
对于后代(正如其他人所提到的)npm build,npm 使用node-gyp 来构建本机 C/C++ Node 插件。它没有被很好地记录下来,因为它通常是自动发生的,但是如果您有兴趣,源代码在这里。
回答by Flimm
The script named as "build" in package.jsonis not special in any way. The only way to get it to run is to call:
名为“build”的脚本package.json在任何方面都没有什么特别之处。让它运行的唯一方法是调用:
npm run-script build
There are some names which are called automatically by npm, but "build" is not one of them. The full list is:
有一些名称由 npm 自动调用,但“build”不是其中之一。完整列表是:
prepublish,publish,postpublishpreinstall,install,postinstallpreuninstall,uninstall,postuninstallpreversion,version,postversionpretest,test,posttestprestop,stop,poststopprestart,start,poststartprerestart,restart,postrestartpreCUSTOMandpostCUSTOMfor custom script names.
prepublish,publish,postpublishpreinstall,install,postinstallpreuninstall,uninstall,postuninstallpreversion,version,postversionpretest,test,posttestprestop,stop,poststopprestart,start,poststartprerestart,restart,postrestartpreCUSTOM和postCUSTOM自定义脚本名称。
回答by mikemaccana
OK, to run a build on it's own, use:
好的,要自己运行构建,请使用:
npm run-script build
回答by Or Gal
I had a problem with npm run buildnot printing anything. ended up using npm run build --verboseto get the output I needed.
我遇到了npm run build不打印任何内容的问题。最终npm run build --verbose用于获得我需要的输出。
回答by Kangcor
Npm buildexpects
Npm 构建期望
A folder containing a package.json file in its root
在其根目录中包含 package.json 文件的文件夹
Try using npm scriptsin your package.json, like the classic npm start
尝试在 package.json 中使用npm 脚本,就像经典的 npm start

