'What is the difference between npm install and npm run build?
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?
Solution 1:[1]
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 installis not the same asnpm run installnpm installinstallspackage.jsondependencies, then runs thepackage.jsonscripts.install- (Essentially calls
npm run installafter dependencies are installed.
- (Essentially calls
npm run installonly runs thepackage.jsonscripts.install, it will not install dependencies.npm buildused to be a valid command (used to be the same asnpm 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 startwhich is the same asnpm run startnpm test==>npm run test
Solution 2:[2]
The main difference is:
npm installis a npm CLI-command which does the predefined thing i.e., as written by Churro, to install dependencies specified insidepackage.json.
npm run %command-name%ornpm 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 casenpm run buildis a custom script command with the name "build" and will do anything specified inside it (for instanceecho 'hello world'given in below examplepackage.json).
Points to note::
One more thing,
npm buildandnpm run buildare two different things,npm run buildwill do custom work written insidepackage.jsonandnpm buildis a pre-defined script (not available to use directly).You cannot specify some thing inside custom build script (
npm run build) script and expectnpm buildto do the same. Try following thing to verify in yourpackage.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.
Solution 3:[3]
npm installinstalls the depedendencies in your package.json config.npm run buildruns the script "build" and created a script which runs your application - let's say server.jsnpm startruns 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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Mike |
| Solution 3 | Chanoch |
