243

What is the difference between npm install and npm run build?

I have noticed in my project that sometimes npm starts failing when npm install is performed, but, upon running npm run build, it works just fine.

How do the inner workings of these two targets namely install and run build differ?

1
  • 2
    The accepted answer as of now is debatable. I think the answers from MKP and CTS_AE, are more relevant at this given time. With due respect, This should come under the notice of moderator &/or the one who asked this question. Commented Sep 19, 2019 at 13:45

4 Answers 4

185

npm install installs dependencies into the node_modules/ directory, for the node project you're working on. You can call install on another node.js project (module), to install it as a dependency for your project.

npm run build does nothing unless you specify what "build" does in your package.json file. It lets you perform any necessary building/prep tasks for your project, prior to it being used in another project.

npm build is an internal command and is called by link and install commands, according to the documentation for build:

This is the plumbing command called by npm link and npm install.

You will not be calling npm build normally as it is used internally to build native C/C++ Node addons using node-gyp.

2
  • 4
    After referring the below link's correct answer, I also think that npm build and npm run build are not the same. IMHO, Maybe the above answer from @churro needs to be corrected. Please correct me if I am wrong. stackoverflow.com/questions/29939697/… Commented Jun 4, 2019 at 16:38
  • So npm install automatically runs npm run build for all the dependencies scripts? Where is that documented?
    – Anthony
    Commented Aug 31, 2022 at 11:08
153

NPM in 2019

npm build no longer exists. You must call npm run build now. More info below.

TLDR;

npm install: installs dependencies, then calls the install from the package.json scripts field.

npm run build: runs the build field from the package.json scripts field.


NPM Scripts Field

https://docs.npmjs.com/misc/scripts

There are many things you can put into the npm package.json scripts field. Check out the documentation link above more above the lifecycle of the scripts - most have pre and post hooks that you can run scripts before/after install, publish, uninstall, test, start, stop, shrinkwrap, version.


To Complicate Things

  • npm install is not the same as npm run install
  • npm install installs package.json dependencies, then runs the package.json scripts.install
    • (Essentially calls npm run install after dependencies are installed.
  • npm run install only runs the package.json scripts.install, it will not install dependencies.
  • npm build used to be a valid command (used to be the same as npm run build) but it no longer is; it is now an internal command. If you run it you'll get: npm WARN build npm build called with no arguments. Did you mean to npm run-script build? You can read more on the documentation: https://docs.npmjs.com/cli/build or https://docs.npmjs.com/cli/v6/commands/npm-build

Extra Notes

There are still two top level commands that will run scripts, they are:

  • npm start which is the same as npm run start
  • npm test ==> npm run test
4
  • 11
    +1 But it's confusing to say npm build doesn't exist anymore. It still exsits in the sense that it's known/special to npm, because it reacts with the warning you mention in the bottom. Essentially it does nothing now. This answer to another question gave the shortest summary on npm build vs npm run build.
    – bluenote10
    Commented Jan 11, 2019 at 6:46
  • I just tried using yarn on a recent project promoted by none other than the folks at Salesforce.. yarn did not work. But NPM worked beautifully
    – bkwdesign
    Commented Dec 9, 2020 at 16:36
  • 1
    Link is broken, here's an updated one: docs.npmjs.com/cli/v6/commands/npm-build Commented Mar 2, 2022 at 0:52
  • So npm install automatically runs npm run build for all the dependencies scripts? Where is that documented?
    – Anthony
    Commented Aug 31, 2022 at 11:08
60

The main difference is:

npm install is a npm CLI-command which does the predefined thing i.e., as written by Churro, to install dependencies specified inside package.json.

npm run %command-name% or npm run-script %command-name% is also a CLI-command predefined to run your custom scripts with the name specified in place of "command-name". So, in this case npm run build is a custom script command with the name "build" and will do anything specified inside it (for instance echo 'hello world' given in below example package.json).

Points to note::

  1. One more thing, npm build and npm run build are two different things, npm run build will do custom work written inside package.json and npm build is a pre-defined script (not available to use directly).

  2. You cannot specify some thing inside custom build script (npm run build) script and expect npm build to do the same. Try following thing to verify in your package.json:

{
    "name": "demo",
    "version": "1.0.0",
    "description": "",
    "main": "index.js",
    "scripts": {
        "build": "echo 'hello build'"
    },
    "keywords": [],
    "author": "",
    "license": "ISC",
    "devDependencies": {},
    "dependencies": {}
}

and run npm run build and npm build one by one and you will see the difference. For more about commands kindly follow npm documentation.

3
  • 13
    Rather than "try to run this", I'd appreciate if you explain in more detail what is the difference between npm run build and npm build. Ok so they're not the same, in what way ? So @Churro's answer is incorrect then ? Commented May 27, 2018 at 7:40
  • 7
    Yeah, what npm build does remains a mystery. Would be nice to know that. As far as I can tell, "npm build will do as written by Churro" is not true. Churro said it would do what's specified in package.json for the build command (as defined in the scripts section I suppose). But that's what npm run build does, and npm build does not run the build command specified in package.json scripts section. Unless there is some other way to define what npm build should do in the package.json, which is not explained here, nor by Churro (nor in the NPM docs, I think, sadly).
    – trollkotze
    Commented Nov 7, 2018 at 16:58
  • 2
    So I tried it and npm build said npm WARN build npm build called with no arguments. Did you mean to npm run-script build?. I think it did nothing. After spending time to read all answers here and trying this myself I still cannot tell what's the purpose of npm build :(.
    – bluenote10
    Commented Jan 11, 2019 at 6:34
7
  • npm install installs the depedendencies in your package.json config.
  • npm run build runs the script "build" and created a script which runs your application - let's say server.js
  • npm start runs the "start" script which will then be "node server.js"

It's difficult to tell exactly what the issue was but basically if you look at your scripts configuration, I would guess that "build" uses some kind of build tool to create your application while "start" assumes the build has been done but then fails if the file is not there.

You are probably using bower or grunt - I seem to remember that a typical grunt application will have defined those scripts as well as a "clean" script to delete the last build.

Build tools tend to create a file in a bin/, dist/, or build/ folder which the start script then calls - e.g. "node build/server.js". When your npm start fails, it is probably because you called npm clean or similar to delete the latest build so your application file is not present causing npm start to fail.

npm build's source code - to touch on the discussion in this question - is in github for you to have a look at if you like. If you run npm build directly and you have a "build" script defined, it will exit with an error asking you to call your build script as npm run-script build so it's not the same as npm run script.

I'm not quite sure what npm build does, but it seems to be related to postinstall and packaging scripts in dependencies. I assume that this might be making sure that any CLI build scripts's or native libraries required by dependencies are built for the specific environment after downloading the package. This will be why link and install call this script.

Not the answer you're looking for? Browse other questions tagged or ask your own question.