`npm build` doesn't run the script named "build" in package.json
Solution 1
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.
Solution 2
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
andpostCUSTOM
for custom script names.
Solution 3
OK, to run a build on it's own, use:
npm run-script build
Solution 4
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
Solution 5
I had a problem with npm run build
not printing anything. ended up using npm run build --verbose
to get the output I needed.
Related videos on Youtube
mikemaccana
I help verify websites for EV HTTPS at CertSimple and have made a bunch of tech products in the past 20 years as a product manager, CTO, lead developer, systems engineer, and technical architect - see https://mikemaccana.com
Updated on July 08, 2022Comments
-
mikemaccana almost 2 years
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.
-
Zaz over 7 yearsWorkaround: use
install
instead. -
Emobe almost 5 yearsThis is basically a huge and unintuitive annoyance of NPM and is one of the reasons I continue to use Yarn. With yarn, I can run any custom script just as a parameter i.e yarn storybook will run the storybook script. In NPM I have to do npm run storybook and on top of that, if I wish to pass any parameters through npm, it requires -- before it, so when comparing yarn storybook --ci to npm run storybook -- --ci, it's a no-brainer to me.
-
-
Jakub Miziołek almost 8 yearsSome packages require a build process. When you're running
npm install
and npm finds a package that has C/C++ bindings or generally sth that needs node-gyp to run then it startnpm build
. You can rebuild those packages by simply runningnpm build
alone. -
Pawel over 7 yearsinternal command should be renamed to
_build
andnpm build
shuould be a shortcut likenpm start
andnpm test
-
aaaidan over 7 yearsI understand that
npm build
won't call my script, and that it's used to build compiled components of a package. I'm still unsure how it goes about doing that: what files does it look for, etc? -
grantwparks about 7 yearsThis of course makes sense, until you look at the docs reference and see that "start" is also listed. yet, if I take out my "start" script from package.json, it doesn't know what to do.
-
Frank Nocke about 7 yearsIf I was to write a package myself, it would still be nice to know, what to do to fill
npm build
with meaning... -
Erhhung about 4 yearsWhat this means is that
npm build .
should work, and, in my case, npm does execute the "build" script in my "package.json" as I hoped.