What is difference between local and global Node package installation? - javascript

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.

Related

do I have to install npm for every new project?

I have NPM installed in my PC how to use it for a new project?
or do I have to install npm for every new project?
NPM is extremely useful, but, when you install it, you install it globally. It comes with Node JS, so when you install Node JS, you should have npm installed(type npm -v to see the version and whether npm is installed).
"npm init" creates a package.json for your folder, which contains all the information about the version number, the name of the project, and some other information. In package.json, you can add dependencies, which basically say your project relies on that npm package.
"npm install" will install all the packages specified in package.json, and, if you want to install something specific, you would type "npm install ".

"npm-run-all" Is Not Recognized As an Internal or External Command

I installed npm-run-all and also configured the environment variable (which may or may not be not required) on my Windows machine but am getting an error:
'npm-run-all' is not recognized as an internal or external command,
operable program or batch file
I am trying to build my current project with npm run build which includes the script where the error is thrown:
npm-run-all -p build-css build-webpack
Do I have to do any additional things to make it run?
Make sure the npm-run-all is in your package.json devDependencies.
If npm-run-all is present in your package.json, run npm i
If not present install it, run: npm i npm-run-all -D
If error is still present, follow these steps:
Remove node_modules folder: run rm -rf node_modules
Install all dependecies: run npm i
Hope this helps!
You may just need to run the following command first (from the directory with the package.json file)
npm install
Please do that like this.
npm i npm-run-all -g
And then this issue will be fixed.
You have a couple of options here, besides installing npm-run-all as a global package as suggested by #Vaibhav in the comments:
1) Create an NPM script
The package.json file has a scripts section which can used to define shortcuts for anything you need to run while you're working on your app. There are some pre-defined scripts, like run or test than can be executed with simply npm start/npm test or you can define anything you like and then run it with npm run my-script-name. You could try:
{
"scripts": {
"start": "npm-run-all -p build-css build-webpack"
}
}
Any NPM module referenced here "just works" (i.e. the path to the executable is resolved under the hood by NPM)
2) NPX
In newer versions of NPM (i.e. >= 5.2 or so), the "NPX" executable is provided. This has a similar effect to running commands inside an NPM script. You would run:
npx npm-run-all -p build-css build-webpack
Again, the path would be automatically resolved.
If you have an older NPM install, you can also install it separately:
npm install -g npx
npm install -g npm-run-all
Works for me.
Double check if npm-run-all is in your package.json devDependencies.
I had same problem while using code editor Brackets.
To resolve the error, I did the following steps.
Add nodejs new system variable to your PC under Control Panel -> System -> Advanced System Settings
;C:\Program Files\nodejs\
After that, re-run command:
npm
I don't know if this would help anyone, but I got this error because I was doing nodemon server.js instead of nodemon server/server.js. I wasn't in the right folder!
Did you reopen the terminal after you installed node?
If you have installed npm with the current terminal window open. Your terminal window will not have loaded the latest path settings (with npm location) to find the npm application to run the command. In this case try below steps .
Try closing the current terminal session.
Reopen a new session.
Try the command again ( will pick up the new path settings with npm installed)
This worked for me.
npm audit fix --force
Also you can try downgrading your autoprefixer, seems version 10.0.0 doesn't work well with postcss
npm i autoprefixer#9.8.6

Is npm more like mvn or like pip?

I'd like to know if Javascript's npm installs dependencies OS-wide like Python's pip (if pip is not using a virtualenv), or if npm installs dependencies more like Java's mvn which stores the things in a local pom.xml file (to me mvn feels more like if it was always in a virtualenv if compared to pip).
So I mainly want to know if it's OS-wide installs or local installs that are performed with npm.
Both!!
npm install -g PACKAGE_NAME will install the package globally.
npm install PACKAGE_NAME will install the package locally in the current folder, under node_modules/.
npm install --save PACKAGE_NAME will install the package locally and save it as a dependency in your package.json.
Checkout https://www.sitepoint.com/beginners-guide-node-package-manager/
if you execute just npm install <dependency name> you are telling node to install package only on your current directory this will also create node_modules in current directory you are installing.
and if you execute the npm install -g <dependency name> with the -g you are saying that you want the node to install the dependency globally and the package will be save on the global node_modules. and most of the global dependencies can be access using CLI
example.
$ npm install mocha
you can execute the command in the current directory execute the npm install or where the node_module is
$ $PWD/node_modules/.bin/mocha -v
if you
$ npm install -g mocha
you can execute the command anywhere in your directory on the terminal
$ mocha -v

how to npm install only devDependencies with node 8.7.x?

The usual way of installing only devDependencies was to use npm install --only=dev (or --only=production if you want only dependencies).
This doesn't work anymore in 8.7. If I run this command, npm will try installing all dependencies. Or at least, it runs a /usr/bin/git ls-remote -h -t on packages that are not in devDependencies. Those packages being in private git repos, the npm install fails for me.
This didn't happen until I upgraded to 8.7.0, from 7.4.0
The npm cli documentation still shows the old way of doing it though.
Is there a new syntax for that option?
From the output of npm help install:
npm install (in package directory, no arguments):
Install the dependencies in the local node_modules folder.
In global mode (ie, with -g or --global appended to the command), it installs the current package context (ie, the current working directory) as a global package.
By default, npm install will install all modules listed as dependencies in npm help 5 package.json.
With the --production flag (or when the NODE_ENV environment variable is set to production), npm will not install modules listed in devDependencies.
So it seems you can install only dependencies with --production; not sure if there's a way to only install devDependencies.

npm install couldnt install angularjs libraries

I have installed mean.io and ran sudo npm install. Actually following commands in sequence
sudo npm install -g meanio
mean init yourNewApp
cd yourNewApp
sudo npm install -g bower
sudo npm install
It is supposed to download and install angularjs libraries into public/system/lib. After doing the above steps public /system/lib is not created due to which when I start the application I get the error
events.js:72
throw er; // Unhandled 'error' event
^
Error: ENOENT, open '/home/santhosh/dev/scaleqa/mean_tut/old mean/temp/myapp/public/system/lib/bootstrap/dist/css/bootstrap.css'
[nodemon] app crashed - waiting for file changes before starting...
Is it something to do with certain npm/angularjs server being down. I have faced this problem earlier also but got fixed on 2nd try and I didn't bother to do more research. This became a big issue when I try to pull my repo into cloud and start the application. public/system/lib is added in .gitignore by default and is expected to be created during npm install.
I get following warnings with sudo npm install
npm WARN package.json mean-connect-mongo#0.4.3 No repository field.
npm WARN cannot run in wd mean#0.3.3 node node_modules/bower/bin/bower install (wd=/home/santhosh/dev/scaleqa/mean_tut/old mean/temp/myapp)
this is link to package.json
The problem maybe related to running npm install as sudo, which can cause problems. As mentioned in another stack overflow question, this can be worked around in a couple ways. But because it looks like this is being run from your home directory, you really shouldn't need to run npm install as root.
Try to issue the same commands, but the last without sudo:
sudo npm install -g meanio
mean init yourNewApp
cd yourNewApp
sudo npm install -g bower
npm install
Note that the reason you may need to run npm install -g <package> using sudo is because by default npm uses /usr/local for global installs, which can be a restricted directory. However, when you install a package locally (without the -g flag) you should not need to run as root.

Categories

Resources