mikemaccana mikemaccana - 5 months ago 7
Node.js Question

`npm build` doesn't run the script named "build" in package.json

For a new module I'm trying to use

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.

Answer

The issue is that npm build is actually 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 short-hand for the fully-qualified run-script, but is only an option when an existing npm command doesn't shadow it, like npm build does. Basically, if an built-in npm command isn't found it will try to find and use any matching one from your "scripts" package.json property.