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

102,893

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 and postCUSTOM 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.

Share:
102,893

Related videos on Youtube

mikemaccana
Author by

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, 2022

Comments

  • mikemaccana
    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
      Zaz over 7 years
      Workaround: use install instead.
    • Emobe
      Emobe almost 5 years
      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.
  • Jakub Miziołek
    Jakub Miziołek almost 8 years
    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.
  • Pawel
    Pawel over 7 years
    internal command should be renamed to _build and npm build shuould be a shortcut like npm start and npm test
  • aaaidan
    aaaidan over 7 years
    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?
  • grantwparks
    grantwparks about 7 years
    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.
  • Frank Nocke
    Frank Nocke about 7 years
    If I was to write a package myself, it would still be nice to know, what to do to fill npm build with meaning...
  • Erhhung
    Erhhung about 4 years
    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.