npm scripts to run packages global or local - javascript

You can think that I will install nodemon with below script:
npm install --save-dev nodemon#1.0.0
So it will install the nodemon package to my project locally.(not global)
I will also add the npm script:
{
"scripts": {
"nodemonscript": "nodemon yourscript.js"
}
}
So if I execute "npm run nodemonscript" it will run the nodemon from my project local node_modules.(not global)
So somehow I will decide to also install nodemon 2.0.0 globally.
I will run this script to install package:
npm install -g nodemon#2.0.0
Finally I have nodemon#1.0.0 from my local project and nodemon#2.0.0 globally.
So if I execute
"npm run nodemonscript"
again from my local project root which have below script:
{
"scripts": {
"nodemonscript": "nodemon yourscript.js"
}
}
Here are my questions:
1)Which version will be execute 1.0.0 or 2.0.0?
2)I want to use always local nodemon package. Which way is more safe?
3)Are all the npm packages have the same behaviour for npm scripts?
4)And my last question is how npx will behaviour for this situation?

Related

What is difference between local and global Node package installation?

I ask this question because I have been installing nodemon with npm and I see the results of installing through the suggested command at first sight, at the right side of the screen:
npm i nodemon
It is different from the installation instructions you can read above, on the Installation section. There we see:
global installation:
npm install -g nodemon
install nodemon as a local project dependency:
npm install --save-dev nodemon
The thing is, what is difference between
npm i nodemon
and
npm install -g nodemon
When I use the first command it tells me typical "nodemon is not recognized as internal or external command, operable program or batch file". To solve it I must install globally.
When you run npm i nodemon nodemon is installed as a local project dependency, to run nodemon on the CLI you would have to provide the pull path to it's installation, typically you would want to make this reference in your project's package.json file's scripts property, for instance:
{
...
"scripts": { "nodemon": "nodemon index.js" },
...
}
This can then be executed by running npm run nodemon.
On the other hand running npm install -g nodemon or npm i -g nodemon installs nodemon on the global scope where it is referenced in your system PATH variable, that way you can easily call nodemon on the CLI and since it's full installation path is referenced in your system PATH variable it would execute like every other CLI command.
Browser is made available to the current project (where it keeps all of the node modules in node modules) after local installation. It will not be available as a command that the shell can resolve until you install it globally with npm install -g module, in which case npm will install it in a location where your path variable will resolve this command. Typically, this is only good for using a module like so var module = require('module');
This documentation will help.

NPM return “bash command not found” when I try to get a package version

I have installed the package.json that includes nodemon package (and others)
#npm list --depth 0
├─┬ nodemon#2.0.7
but the command #nodemon -v return a -bash error: "nodemon command not found”
I cant start my server.js with nodemon, the same error, but all works with #node server.js
Any idea? Thnks
With a local install
You can use npx nodemon filename.js
If you want to install it globally
With npm npm install nodemon -g or with yarn yarn global add nodemon, that way you can use nodemon directly (nodemon filename.js)

How to install nodemon Nodejs on macOS? nodemon keeping show : command not found

Currently I already install nodemon with command npm install -g nodemon. And I got Permissions issue, so I do command with sudo npm install -g nodemon and i did it. But when I make "nodeman" command was always show nodemon: command not found.
If for any reasons you are unable to set a Global PATH then under your current project directory, run
npm install nodemon --save-dev
then under "scripts" in your package.json file, add "start": "nodemon app.js" like this -
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "nodemon app.js"
}
then run
npm start
If you need to install nodemon globally on mac OS, try
sudo npm install -g nodemon.
Then you'll have to enter your password. Once the installation is completed successfully, run
nodemon -v
to check nodemon version on terminal.
According to this, Create a new directory to store your global packages. So that there is no permission issue.
mkdir ~/.npm-global
npm config set prefix '~/.npm-global'
Edit your .profile or .bash_profile to add the new location to your PATH:
export PATH=~/.npm-global/bin:$PATH
Then install the package without sudo:
npm install -g nodemon
If you want to install global nodemon use SUDO, because if you need to be a global user, you need to be a super user
The other answer is correct but my advice is that it's better to not install packages globally if you can help it, this makes your application self sufficient without relying on the environment and avoids versioning issues between applications.
npm install -D nodemon
You can now execute nodemon from scripts in package.json:
"scripts": {
"start": "nodemon src/index.js"
}
Or you can execute it yourself using npx if you're in that directory from the terminal. npx executes local scripts, e.g. npx nodemon --inspect ./src/index.js 8080
Just run these commands and the error will get solved.
Specially for MAC People:
sudo chown -R $(whoami) $(npm config get prefix)/{lib/node_modules,bin,share}
Enter your laptop password
npm install i -g nodemon or npm install -g nodemon
All set .....🎉

babel-cli issues with npm

I am using babel to build a part of my library and I'm running into issues when i run babel commands through npm.
I have an npm script called build that calls:
{
"prebuild": "rm -rf && mkdir dist",
"build": "babel src/index.js -o dist/index.js"
}
I have run the actual babel command itself in my command line and it works.
However when I do npm run build from my command line it says
The CLI has been moved into the package 'babel-cli'
npm also says that it is that specific line that is failing.
I have already tried the following:
Restart my terminal.
Install babel v5 because I read that v6 split a lot of its functionality.
npm rebuild.
Delete my node_modules and npm install
Any other ideas? as to why npm fails at running this command?
Install babel#5 globally (npm install babel#5 --global) as well as locally: npm install babel#5 --save-dev

NPM install + use local modules? on windows

I created a package.json and added the needed dependencies (grunt, bower, jasmine etc)
when I run
npm install
it correctly installs all the modules in
$pwd/node_modules/*
however when I try to USE those modules:
bower init; grunt init
i get
sh.exe: (bower / grunt / w.e) Command not found
I can solve this by using
npm install -g (package name)
but my understanding is the "-g" makes the install global? (is that correct?)
I want to be able to use the packages i installed locally...is this possible?
You can use npm run-scripts to create a command which will run the local copies of bower and grunt that you have installed.
In package.json, add a key like this:
"scripts": {
"init": "bower init; grunt init"
}
Then run the command npm run init.

Categories

Resources