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

6

162

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.

Gail answered 29/4, 2015 at 9:33 Comment(2)
Workaround: use install instead.Dairyman
This 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.Selfdriven
I
282

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.

Imam answered 7/5, 2015 at 21:55 Comment(6)
Some 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 start npm build. You can rebuild those packages by simply running npm build alone.Maxentia
internal command should be renamed to _build and npm build shuould be a shortcut like npm start and npm testOlympe
I 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?Phenazine
This 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.Spiffy
If I was to write a package myself, it would still be nice to know, what to do to fill npm build with meaning...Agace
Link do docs brokenShang
D
21

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.
Drucilladrucy answered 24/6, 2016 at 11:4 Comment(0)
G
5

OK, to run a build on its own, use:

npm run-script build
Gail answered 29/4, 2015 at 9:44 Comment(2)
On "its" own. its-not-its.infoArchle
@colinmoock thank you! My mum was an English teacher, she'd be ashamed :D. Corrected!Gail
L
2

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

Lymphangial answered 29/4, 2015 at 9:43 Comment(1)
What 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.Chrysoberyl
T
2

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

Temporary answered 8/11, 2015 at 7:22 Comment(0)
O
0

Don't forget to check the directory

  1. First, im assuming you have made a folder named "React" and opened in VS code.
  2. Then, you have done..(supposing) "npx create-react-app test"
  3. after installing all the packages and files, CHECK "package.json" file, if it exists then well sorted, if not, then re-install.
  4. After that, check the directory, it should be in "test". To do that, run command "cd test"
  5. Then, run "npm run start"
  6. Then, run "npm run build". And, all set ;)
Orsini answered 9/2 at 16:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.