What is the difference between npm install and npm run build? - node.js

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

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::
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.

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.

Related

Why can npm run babel-node from script but not as npm exec?

In a lerna monorepo, babel is installed only at the root package level.
When running npm exec babel-node from root/packages/packageA, it fails with:
npm ERR! could not determine executable to run
From the same directory, running npm run test, where test is just set to babel-node opens the babel-cli.
I.e. why can npm resolve the path to the root node_modules' bin when run as a script but cannot when run from the terminal directly?
EDIT:
I.e. https://github.com/bishonen/newproject/tree/master/packages/someapp
when running npm run test-babel, it will work from any of the 3 directories which contain a package.json.
When running npm exec babel-node, it will only work from the root directory.
Workspaces support for both npm run and npm exec would be supported in npm#7.7.0 or greater. See the details here
You can use the feature updating npm
npm i -g npm#7
I try it and it works in your code
https://github.com/bishonen/newproject/tree/master/packages/someapp
I use npm v7.12.0

How to fix `sh: ___: command not found` for NPM Modules

I'm relatively new to Node development and I'm trying out various modules, frameworks, etc.. I'm on macOS Catalina. I've changed my NPM root and $PATH while trying to get things to work, and I think I broke something, though I'm not exactly sure what. My issue is very specifically with NPM, which is having a persistent problem running installed modules both locally and globally. When I try to install a dev tool like Nodemon or a framework like Electron, I get error codes like:
sh: electron: command not found
npm ERR! code ELIFECYCLE
npm ERR! syscall spawn
npm ERR! file sh
npm ERR! errno ENOENT
npm ERR! electron-quick-start#1.0.0 start: `electron .`
npm ERR! spawn ENOENT
npm ERR!
npm ERR! Failed at the electron-quick-start#1.0.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
I've consulted a lot of resources and a lot of questions on this site, but I'm not experienced enough to accurately troubleshoot this issue and identify what's going on. While I'm shooting in the dark, I may be fixing one thing and breaking another, so I'm coming here to see if I can get a comprehensive answer.
I'll add that I've found a few workarounds, if this makes the issue clearer at all. I can get some modules (in this example Nodemon) to run by adding this "dev" script to the package.json:
"scripts": {
"dev": "node ./node_modules/.bin/nodemon server.js"
}
So I can access local project modules by specifying the path of the Module, but I can't just say "nodemon server.js", either in the dev script or from the command line, where it should have been installed globally. If I look in the specified npm -g root folder, the packages I'm trying to run are definitely in there, so it's not an installation issue.
Any guidance y'all have is greatly appreciated, as I don't know what to make of all this.
There are 2 installation style, local and global, on npm.
It is highly recommended to not mix both on the same environment, at least for a given module.
Global installation
e.g. npm install -g <module name>
The executable(s) provided by the module you have installed will be added into the place which PATH environment variable includes.
You can then use this command everywhere (here electron or nodemon), but you cannot use different versions for each project in your environment. You can isolate this behavior with nvm, or better in a container.
Local installation
e.g. npm install --save <module name>
The executable(s) will be installed into the project node_modules directory and saved in the package.json.
You can then use different versions of the module for each project and easily manage it with package.json.
PATH environment variable does not include there, so you have to use ./node_modules/.bin/<command>, as you did in your workaround, or $(npm bin)/<command>.
You can also use any command provided by the module in npm-scripts even if it's a local install because npm adds ./node_modules/.bin to PATH before npm run-script command.
See also: https://docs.npmjs.com/cli/v7/commands/npm-run-script

Can anyone explain this error when I run "npm run ng new app"?

So I'm trying to make a new angular app for the first time, and I installed it using npm i -g #angular/cli. When I try to make a new app using npm run ng new app, it gives me this error:
npm ERR! path E:\ddii\package.json
npm ERR! code ENOENT
npm ERR! errno -4058
npm ERR! syscall open
npm ERR! enoent ENOENT: no such file or directory, open 'E:\ddii\package.json'
npm ERR! enoent This is related to npm not being able to find a file.
npm ERR! enoent
npm ERR! A complete log of this run can be found in:
npm ERR! C:\Users...\AppData\Roaming\npm-cache_logs\2018-09-10T20_22_50_118Z-debug.log
In short, you are running the command incorrectly. Remove the npm run from the front of your command, and it should work.
When you say npm run blah, npm does a lookup in your package.json for a script called blah or in your case ng. So... if you are running npm run ng new app, npm wants there to be a package.json in your current directory, and in that package.json, npm expect a script called ng. So if you don't have a package.json in your current dir, then you are going to get an error.
Instead, close your terminal, and open a new terminal and run simply ng new app.
I struggled to find this answer myself, as the other answers on this question do not address the real underlying issue.
All the other answers suggest to install ng GLOBALLY using (npm i -g ...), with the side effect that a symlink to ng will be on your PATH.
Based on OP's question, I do concede he was most likely attempting to scaffold a new Angular app from the GLOBALLY installed ng. So this handles 90% of people's questions, and is all fine and nice.
I, however, want to use the LOCALLY INSTALLED ng, which is why I have an npm-script in my package.json for it:
{
...
"scripts": {
...
"ng": "ng",
...
}
If this describes your use case like it does mine, then read on.
npm run ng update succeeds for me, whereas other more complex commands with more arguments—including command-line switches—fail outright (npm run ng -- update #angular/cli --migrate-only tslint-version-6).
The answer lies in that you must delimit the start of arguments being passed to npm run-script with -- (see https://stackoverflow.com/a/14404223/1438576):
npm run ng -- update #angular/cli --migrate-only tslint-version-6
So in OP's case, assuming he already had a locally-installed copy of ng with a package.json (which I admit is doubtful), he could have done:
npm run ng -- new app
Anyway, hope this helps others.
If you want to run it without the npm run ..., you need to install ng globally, I would do npm install -g #angular/cli, however I'm running linux, for windows I've found this thread ng is not recognized as an internal or external command
It should help you install a global version of angular-cli, you'll basically do two things:
Install globally angular-cli (if you don't have it).
Make sure it's in your environment variable's PATH.
Check the question answers, details are there.
Could help for future references
At least for me this work.
Sometimes, the reference to the file ng.cmd (in case of Win users) is not well formed and you have to call it and pass the arguments directly
e.g.
< path-where-ng.cmd-file-is-located >\ng.cmd new app
Running calling ng.cmd file directly
It says: ' "ng" is not recognized as an internal or external command, operable program or batch file.'
And it works even if you do not have admin permissions for any reason

What is the difference between `npm run-script build` vs `npm build`?

When I run npm build, it gives this error:
npm WARN build `npm build` called with no arguments
So, what's the difference between npm run-script build and npm build?
npm run-script is a way to execute arbitrary commands specific to the project/package. Check your applicable package.json file, which will have defined what happens when you execute npm run-script build for that package. It may also include what happens when you run common commands, such as npm run-script test.
As you can see in the documentation for npm run-script, this arbitrary command can include arguments, which you need to refer to your package.json to learn more about.
npm build is not a unique command to the package, and is a native command that ships with npm, as you can see in its documentation.
The best answer is in this SO article.
Basically...
npm run == npm run-script
Plus, certain common tasks are aliased so that the following are true...
npm test == npm run-script test
npm build == npm run-script build
For common tasks, use...
npm start
npm build
npm test
npm restart
And for all others, use...
npm run <my-task>
TLDR
npm build is an old CLI command that npm stopped exposing via their CLI after version 6 of the CLI.
"build" is a common name chosen by developers for the user-defined script that builds their project. Developers define this in their package.json file and run with some variant of npm run-script build. If this user-defined script is not defined in package.json, the npm CLI will throw an error, just as it does when a user attempts to pass any other non-existent user-defined script to npm run-script.
Alias in Wonderland
First of all, I'll try not to go down the rabbit hole, but let me get something out of the way. At the time of writing this, run is just an alias for run-script, as are rum and urn, believe it or not. In the remainder of this answer, I will just use run-script, since it seems to be the "main" name (and not an alias) for this command. But in any of the usages below, feel free in your mind to replace run-script with run, or even rum or urn, if you are feeling quirky.
npm is a CLI
OK great, what next? Well, to avoid confusion, let's separate our concerns and first focus on the difference between npm COMMAND and npm run-script SCRIPT, in general. Here I am just using COMMAND and SCRIPT to denote some arbitrary command/script, respectively.
First, think about what npm is. It's a CLI tool. And like any other CLI tool you can think of, it has some built in commands or "verbs". For example, if we do npm ls, we can see a list of installed packages. Here ls is the COMMAND or the "verb" passed to npm.
The npm run-script command
One of the commands i.e. verbs that the npm CLI supports is run-script. This particular command will:
[run] an arbitrary command from a package's "scripts" object. If no "command" is provided, it will list the available scripts.
(source)
So for any user-defined SCRIPT you have in your package.json file, you can run it, using npm, by doing:
npm run-script SCRIPT
SCRIPT can be almost anything you like*. It's user defined. You can call a command build if you like, or you can call it billy or bilbo. Clearly, you want to call it something sensible. And that's why a lot of developers use the term build for a script which builds their project.
*There are some naming restrictions on the SCRIPT so it's not totally free-form. See here.
npm build and npm run-script build
Now that we understand npm is a CLI tool, we can finally tackle the problem of npm build. Here, we are passing build as the verb to the npm CLI. This is completely different to passing build as the name of a custom-script to run with npm run-script build.
Now, the question is, is the build command even a valid verb of the npm CLI? The answer is: it depends on the version of npm you are using:
Version 8: There is NO build command.
Version 7: There is NO build command.
Version 6: There is a build command.
So npm build is only valid in version 6, and possibly earlier versions, of the npm CLI.
It's likely that NPM intentionally removed the build command from their CLI to avoid confusion. Many developers use build as the name of one of their user-defined scripts in package.json, so having another build verb in the npm CLI causes the two to be confused - hence questions like this one.
Tests
To confirm that npm build and npm run-script build are indeed different, I tested them on Docker Playground for various Node/npm versions. Here are the results:
Node Version
npm version
Result of npm build
Result of npm run-script build
node:10.0.0-slim
npm#5.6.0
No output (command apparently succeeded but didn't seem to do anything)
npm ERR! missing script: build
node:12.0.0
npm#6.9.0
No output (command apparently succeeded but didn't seem to do anything)
npm ERR! missing script: build
node:18.6.0
npm#8.13.2
Unknown command: "build" (the command failed)
npm ERR! missing script: build
Old Source of Confusion
While writing this answer, I discovered a bug in the npm version 6 docs, which, like many of us confused folk visiting this SO question, were erroneously conflating npm build with npm run-script build and adding to the confusion. I have since submitted a PR for this issue which has been merged. Hopefully it will be reflected in the npm docs soon.
Hopefully the links in this answer stay alive for a while, but please comment if they go stale.
In short "npm build" is a inbuilt command or as NPM creator calls it... it is a lifecycle event (predefined) where as npm run-script is a task that executes a script and hence can be defined by user differently from the lifecycle event 'build'.

npm can't find package.json

I'm trying to install the dependencies of some example: npm's express 2.5.8 that I've downloaded, but all of the apps throw the same error:
c:\node\stylus>npm install -d
npm info it worked if it ends with ok
npm info using npm#1.1.1
npm info using node#v0.6.11
npm ERR! Couldn't read dependencies.
npm ERR! Error: ENOENT, no such file or directory 'c:\node\stylus\package.json'
npm ERR! You may report this log at:
npm ERR! <http://github.com/isaacs/npm/issues>
npm ERR! or email it to:
npm ERR! <npm-#googlegroups.com>
npm ERR!
npm ERR! System Windows_NT 6.1.7600
npm ERR! command "C:\\Program Files (x86)\\nodejs\\\\node.exe" "C:\\Program File
s (x86)\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "install" "-d"
npm ERR! cwd c:\node\stylus
npm ERR! node -v v0.6.11
npm ERR! npm -v 1.1.1
npm ERR! path c:\node\stylus\package.json
npm ERR! code ENOENT
npm ERR! message ENOENT, no such file or directory 'c:\node\stylus\package.json'
npm ERR! errno {}
npm ERR!
npm ERR! Additional logging details can be found in:
npm ERR! c:\node\stylus\npm-debug.log
npm not ok
The blockage appears to be:
no such file or directory 'c:\node\stylus\package.json
Did I miss a step that creates the package.json ?
I'm running:
Windows 7 64 bit
npm 1.1.1
node 6.11
express 2.5.8
I think, npm init will create your missing package.json file. It works for me for the same case.
In your project's folder, you need to initialize the package.json file by running the following in the terminal:
npm init
After that, you should be able to install any packages as you would expect, like express:
npm install express
Shout out to Deepali's answer.
I'll be brief but deadly. :) install -d will not work for you. It's simple. Try
$ npm install -g express
Follwing the below steps you well get package.json file.
npm --version
npm install express
npm init -y
If Googling "no such file or directory package.json" sent you here, then you might be using a very old version of Node.js
The following page has good instructions of how to easily install the latest stable on many Operating systems and distros:
https://github.com/joyent/node/wiki/Installing-Node.js-via-package-manager
Beginners usually try the npm command from random locations.
After downloading or creating a project, you have to cd into this project folder. Inside is the file package.json.
cd <path_to_project>
npm install
Use the command in win7/win8/win10 (CD) for moving folders:
Enter your projects folder
Run: npm install -d
try re-install Node.js
curl -sL https://deb.nodesource.com/setup_4.x | sudo -E bash -
sudo apt-get install -y nodejs
sudo apt-get install -y build-essential
and update npm
curl -L https://npmjs.com/install.sh | sudo sh
Node comes with npm installed so you should have a version of npm. However, npm gets updated more frequently than Node does, so you'll want to make sure it's the latest version.
sudo npm install npm -g
Test:
npm -v //The version should be higher than 2.1.8
After this you should be able to run:
npm install
It may be very evident,
but try to launch CMD (for Windows) from the project folder,
where your package.json file is located.
Do not launch CMD from System or from "Search bar" in Win or
move to your project folder with help of cd command and then launch npm start.
I had a similar problem with npm. The problem was that I had the project inside two folders of the same name. I resolved it by renaming one of the folders to something else (outer folder recommended).
It by itself says that package.json is not available in your project.
So, to create package.json, use the following steps:
open command prompt on your project directory
npm init (it will ask you to enter lots of entries like name, version, desc, etc., enter some random values and click enter).
type yes and click enter
Now try again.
Go inside the project folder and check whether the package.json file does exist.
If you are creating the project using Visual Studio Angular project, make sure you run this command inside the ClientApp Folder. there is a good chance, you could be looking for project.json file outside the ClientApp folder.
if the package.json file in the project directory is missing then you can create it by
npm init.
if the package.json file is already created in the project directory then there is a possibility that you are not running your project from the right path.
Use cd your-project-path in the terminal and then run your project from there.
npm init -y
use this command, it will automatically create package.json file with all your machine information.
I found myself here trying to resolve the same error message:
npm ERR! message ENOENT, no such file or directory 'c:\<some_folder>\package.json'
The error could be due to two reasons:
You do not have the package.json
You have the package.json, but you are running npm start in the wrong folder
To troubleshoot the first cause, you need to create a package.json using:
npm init
To fix the second cause, make sure the folder you are running the npm start command is the same folder as the package.json
I was experiencing this identical error and terminal (foolishly, on my part) was in a parent directory instead of the correct one (where package.json was correctly located).
All I did in terminal was cd [insert correct directory name here, overwriting brackets] and that corrected the problem.
My issue was I didn't have a package.json file for some reason. After I wget this file into my directory, I was able to run npm install
https://raw.githubusercontent.com/twbs/bootstrap/master/package.json
Thank you!
I also tried many options for this. I am also using windows.This command helped and saved my time:
npm install -g npm#lts
ok, try to go to the home "user#user:~$ " (cd + enter key), and npm install -g your your_module.
just install any package you want with -g
npm install -g express
For the following command
sudo npm install react browserify watchify babelify --save-dev
I got same error
saveError ENOENT: no such file or directory, open
'/Users/Path/package.json'
But when I run the command
sudo npm install -gd react browserify watchify babelify --save-dev
then no missing file or directory message appeared.
I have run npm install -y to skip the question step for creating the missing file package.json, y means yes
I was also facing same issue while installing typescript. I just initialized an package.josn file by the following command
npm init -y
And then i installed my typescript
npm install -g -typescript
http://blossomprogramming.blogspot.com/
Please check the directory or the folder in which you're installing your new package.
This happened to me as well,
My whole project was in a subdirectory and I was trying to install in the main directory. After checking the whole thing I found out that I had to install in the subdirectory where my project files and package.json files are located and it's done.
Hope this helps...
the previous tips do not pay attention to any change for me. at the end this works for me:
delete the local repo folder
git clone https ... myRebo.git
npm install
npm start
hope it helps somebody
I got this error because I was trying:
npm install --save-exact express#4.17.1 cors#2.8.5 express-bearer-token#2.4.0
#okta/jwt-verifier#1.0.0 mongoose#5.6.7
which I got from: https://developer.okta.com/blog/2019/09/11/angular-mongodb
I had to separate the packages and run them one by one. I think the backslash caused problem.
Adding -g before the package name worked for me. Looking for documentation to explain why this works..
Generate package.json without having it ask any questions. I ran the below comment in Mac and Windows under the directory that I would like to create package.json and it works
$ npm init -y
Wrote to C:\workspace\package.json:
{
"name": "workspace",
"version": "1.0.0",
"description": "",
"main": "builder.js",
"dependencies": {
"jasmine-spec-reporter": "^4.2.1",
"selenium-webdriver": "^4.0.0-alpha.5"
},
"devDependencies": {},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}

Resources