`npm build` doesn't run the script named “buil

2019-01-08 09:12发布

For a new module I'm trying to use npm build without gulp / Grunt / other specialised build tools.

"scripts": {
  "build": "node build.js"
},

My build.js is simply

console.log('Hello')

However, running

npm build

Simply exits without printing anything, with a status of 0.

Running:

npm install

Also does all the normal things, but does not run build.js either.

How can I make npm run my build script?

Edit: even simple bash commands don't seem to work, eg

"scripts": {
    "build": "touch TESTFILE"
},

Doesn't make a file with that name.

5条回答
smile是对你的礼貌
2楼-- · 2019-01-08 09:16

OK, to run a build on it's own, use:

npm run-script build
查看更多
走好不送
3楼-- · 2019-01-08 09:18

Unfortunately npm build is already an internal command, as described in the docs:

This is the plumbing command called by npm link and npm install. It should generally not be called directly.

Because that command already exists, it always shadows over your "build": "node build.js".

The fully-qualified way to run your own script is with run-script or its alias run:

$ npm run build

npm start and others are the short-hand way, but is only an option when an existing npm command doesn't shadow it, like npm build does.


For posterity (as others have mentioned) npm build is 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.

查看更多
smile是对你的礼貌
4楼-- · 2019-01-08 09:21

Npm build expects

A folder containing a package.json file in its root

Try using npm scripts in your package.json, like the classic npm start

查看更多
趁早两清
5楼-- · 2019-01-08 09:32

I had a problem with npm run build not printing anything. ended up using npm run build --verbose to get the output I needed.

查看更多
Viruses.
6楼-- · 2019-01-08 09:39

The script named as "build" in package.json is not special in any way. The only way to get it to run is to call:

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:

  • prepublish, publish, postpublish
  • preinstall, install, postinstall
  • preuninstall, uninstall, postuninstall
  • preversion, version, postversion
  • pretest, test, posttest
  • prestop, stop, poststop
  • prestart, start, poststart
  • prerestart, restart, postrestart
  • preCUSTOM and postCUSTOM for custom script names.
查看更多
登录 后发表回答