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?
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.
npm build
no longer exists. You must call npm run build
now. More info below.
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.
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.
npm install
is not the same as npm run install
npm install
installs package.json
dependencies, then runs the package.json
scripts.install
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-buildThere 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
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 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 build
is 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 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).
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.