How to specify the path of `package.json` to npm? - node.js

I use npm scripts to build my project. I'd like to be able to run the scripts from a different directory. That is, instead of doing the following:
cd project;
npm run build;
cd ..
...I'd like to simply do something like:
npm run build -config project/package.json;
or
npm run build -wd project;
Is this possible?

Using --prefix worked for me:
npm --prefix /path/to/project run build
Where path/to/project is the directory where your package.json with build command defined.

to change the path of package.json that npm gets
this didnt work for me
npm --prefix /path/to/project run build
but this did
npm --prefix /path/to/project run
but this does not permanently change it
for example you can say
npm --prefix /path/to/project run test

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

Node & Yarn Command Means

I am trying to found
npm run install:global
npm install -g win-node-env
yarn && npm run dev:build && npm run dev:serve
What does this three command means ?
I tried to search google but not got relative info.
Without any extra info...
1:
npm run runs the next argument defined in the "scripts" property of your package.json file. So, npm run install:global will execute whatever the corresponding script entry by that name. If your package.json file has this entry (and it should):
package.json:
"scripts": {
"install:global": "npm install -g whatever-the-script-is"
}
2:
npm install -g win-node-env will globally install the win-node-env package, so that it's accessible from a terminal session by typing whatever win-node-env exposes.
3:
yarn && npm run dev:build && npm run dev:serve
Yarn is an alternative for npm. This command would run yarn, then the script in package.json that correspond to dev:build and immediately after run dev:serve.
npm run install:global
Run specified npm script
npm install -g win-node-env
Install package globally
yarn && npm run dev:build && npm run dev:serve
same as
npm install && npm run dev:build && npm run dev:serve
Install local packages and run script
This is how package.json might look like
{
"name": "demo"
"scripts": {
"install:global": "npm install -g my-unrealistic-package"
"dev:build": "echo \"build something\"",
"dev:serve": "echo \"serve something\""
}
}

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

How to run "npm publish" from outside of package directory

I want to run npm publish in automated script .
I try --prefix option , it does not work :
npm publish --prefix /path/to/package;

nodemon not working: -bash: nodemon: command not found

I'm on a Mac running El Capitan. I have node v5.6.0 and npm v3.6.0.
When I try to run nodemon, I get:
-bash: nodemon: command not found
I thought this may mean that I didn't have nodemon installed, so when I tried to install it using...
sudo npm install -g nodemon
...I get this:
npm ERR! Darwin 15.2.0
npm ERR! argv "/usr/local/bin/node" "/usr/local/bin/npm" "install" "-g" "nodemon"
npm ERR! node v5.6.0
npm ERR! npm v3.6.0
npm ERR! path /usr/local/bin/nodemon
npm ERR! code EEXIST
npm ERR! Refusing to delete /usr/local/bin/nodemon: ../lib/node_modules/nodemon/nodemon.js symlink target is not controlled by npm /usr/local
npm ERR! File exists: /usr/local/bin/nodemon
npm ERR! Move it away, and try again.
npm ERR! Please include the following file with any support request:
npm ERR! /Users/brianeoneill/npm-debug.log
If it makes a difference, I'm trying to run nodemon on a project that uses Express v4.13.1
Thanks for any help you can offer!
I tried the following, and none worked:
npm uninstall nodemon
sudo npm uninstall -g nodemon
What did work was:
sudo npm install -g --force nodemon
If you want to run it locally instead of globally, you can run it from your node_modules:
npx nodemon
From you own project.
npx nodemon [your-app.js]
With a local installation, nodemon will not be available in your system path. Instead, the local installation of nodemon can be run by calling it from within an npm script (such as npm start) or using npx nodemon.
OR
Create a simple symbolik link
ln -s /Users/YourUsername/.npm-global/bin/nodemon /usr/local/bin
ln -s [from: where is you install 'nodemon'] [to: folder where are general module for node]
node : v12.1.0
npm : 6.9.0
I'm using macOS/Linux, the solution that works for me is
npx nodemon index.js
I have tried every possibility, like uninstalling and installing nodemon, installing nodemon globally. restart the terminal, but it won't work.
don't try such things to waste your time.
in Windows OS run:
npx nodemon server.js
or add in package.json config:
...
"scripts": {
"dev": "npx nodemon server.js"
},
...
then run:
npm run dev
I had the same exact problem, expect for Windows OS.
For me, running
npm install -g nodemon --save-dev
(note the -g) worked.
Maybe somebody else who has this problem on Windows will have the same solution.
FOR WINDOW USERS
I tried every possible way but nothing worked for me.
What worked was:-
npx nodemon server
FOLLOWING WILL BE THE OUTPUT:-
For mac Users, use npx nodemon index.js
...
"scripts": {
"start": "npx nodemon index.js"
},
...
> my-project#1.0.0 start
> npx nodemon index.js
Need to install the following packages:
nodemon
Ok to proceed? (y)
[nodemon] 2.0.13
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node index.js`
Server Started on Port 8000
sudo npm install nodemon --save-dev
Next package.json on and
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "nodemon app.js"
}
Type on terminal (command prompt)
npm start
Install nodemon:
sudo npm install -g nodemon
Run server:
sudo nodemon server.js
I ran into the same problem since I had changed my global path of npm packages before.
Here's how I fixed it :
When I installed nodemon using : npm install nodemon -g --save , my path for the global npm packages was not present in the PATH variable .
If you just add it to the $PATH variable it will get fixed.
Edit the ~/.bashrc file in your home folder and add this line :-
export PATH=$PATH:~/npm
Here "npm" is the path to my global npm packages . Replace it with the global path in your system
In macOS,
I fixed this error by installing nodemon globally
npm install -g nodemon --save-dev
and by adding the npm path to the bash_profile file. First, open bash_profile in nano by using the following command,
nano ~/.bash_profile
Second, add the following two lines to the bash_profile file (I use comments
"##" which makes it bash_profile more readable)
## npm
export PATH=$PATH:~/npm
For nodemon: not found command
I tried with many links but was not working then i tried with the below steps it worked fine.
Follow this step it worked for me.
step1 : sudo su
step2 : npm install -g nodemon --save-dev
It is working fine.
Just in case for those using Windows, you don't need sudo
npm i -g nodemon
sudo su
then
npm install nodemon
worked for me
Just writing what did worked for me - (on Windows machine, installing node locally to the project)
if you do not want to install it globally (i.e without -g flag) you have to use
npx nodemon app
where app is your app.js is your program file to launch.
Make sure you own root directory for npm so you don't get any errors when you install global packages without using sudo.
procedures:-
in root directory
sudo chown -R yourUsername /usr/local/lib/node_modules
sudo chown -R yourUsername /usr/local/bin/
sudo chown -R yourUsername /usr/local/share/
So now with
npm i npm -g
you get no errors and no use of sudo here.
but if you still get errors confirm node_modules is owned again
/usr/local/lib/
and make sure you own everything
ls -la
now
npm i -g nodemon
will work!
Since Node v18.11.0 there is running in 'watch' mode using
node --watch
which restarts the process when an imported file is changed.
https://nodejs.org/en/blog/release/v18.11.0/
Following commands worked for me in my case
Open Windows Powershell and Run series of following Commands,
Get-ExecutionPolicy -List
Set-ExecutionPolicy Unrestricted
*Press Y for YES*
Set-ExecutionPolicy Unrestricted -Force
Here you Go.
In Windows git bash, I fixed it by restarting git bash
Put --exec arg in single quotation.
e.g. I changed "nodemon --exec yarn build-langs" to "nodemon --exec 'yarn build-langs'" and worked.

Resources