npm install by default tries to install globally - node.js

When performing npm install on a folder, the package is installed on c:\Users\<user>\node-modules instead of .\<project folder>\node-modules
I've tried to update npm config save=false but this didn't solve the problem
PS C:\Users\danielk\Documents\udemy_nodejs\FirstExpressApp> npm install express
npm WARN danielk No description
npm WARN danielk No repository field.
npm WARN danielk No license field.
+ express#4.17.1
updated 1 package and audited 126 packages in 2.004s
found 0 vulnerabilities
PS C:\Users\danielk\Documents\udemy_nodejs\FirstExpressApp> dir
Directory: C:\Users\danielk\Documents\udemy_nodejs\FirstExpressApp
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 21/08/2019 7:54 AM 27 app.js
When doing npm install express in the project folder, I would expect the node-modules subfolder to be created in the project folder and the express module to be installed in the node-modules subfolder. However its created in C:\Users\danielk\node-modules.
Could anyone help on what is wrong and how this can be fixed?

When you do npm install, npm actually installs the package to the "npm project" you are currently in. See the following example:
eric_#Eric-Dev-Laptop MINGW64 ~/Desktop/mixed/stackoverflow
$ npm init
This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sensible defaults.
See `npm help json` for definitive documentation on these fields
and exactly what they do.
Use `npm install <pkg>` afterwards to install a package and
save it as a dependency in the package.json file.
Press ^C at any time to quit.
package name: (stackoverflow)
[...Truncated...]
Is this OK? (yes)
eric_#Eric-Dev-Laptop MINGW64 ~/Desktop/mixed/stackoverflow
$ mkdir child
eric_#Eric-Dev-Laptop MINGW64 ~/Desktop/mixed/stackoverflow
$ cd child/
eric_#Eric-Dev-Laptop MINGW64 ~/Desktop/mixed/stackoverflow/child
$ npm install express
npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN stackoverflow#1.0.0 No description
npm WARN stackoverflow#1.0.0 No repository field.
+ express#4.17.1
added 50 packages from 37 contributors and audited 126 packages in 3.021s
found 0 vulnerabilities
eric_#Eric-Dev-Laptop MINGW64 ~/Desktop/mixed/stackoverflow/child
$ ls
eric_#Eric-Dev-Laptop MINGW64 ~/Desktop/mixed/stackoverflow/child
$ ls ..
child/ node_modules/ package.json package-lock.json
eric_#Eric-Dev-Laptop MINGW64 ~/Desktop/mixed/stackoverflow/child
$ ls ../node_modules/
accepts/ escape-html/ mime/ safer-buffer/
array-flatten/ etag/ mime-db/ send/
body-parser/ express/ mime-types/ serve-static/
bytes/ finalhandler/ ms/ setprototypeof/
content-disposition/ forwarded/ negotiator/ statuses/
content-type/ fresh/ on-finished/ toidentifier/
cookie/ http-errors/ parseurl/ type-is/
cookie-signature/ iconv-lite/ path-to-regexp/ unpipe/
debug/ inherits/ proxy-addr/ utils-merge/
depd/ ipaddr.js/ qs/ vary/
destroy/ media-typer/ range-parser/
ee-first/ merge-descriptors/ raw-body/
encodeurl/ methods/ safe-buffer/
What happens is, I made a stackoverflow folder, initialized an "npm project" there with npm init, and cd into child folder. When I do npm install express inside, the express module will inadvertently get installed to stackoverflow/node_module. This is such that when you are making a program, e.g. myprogram, and even when you are in some subfolder inside (e.g. myprogram/lib/) and you perform npm install, the module will still be installed to myprogram.
Comparing this to your case, this is probably because your C:\Users\danielk\ is already an npm project, so when you are in C:\Users\danielk\Documents\udemy_nodejs\FirstExpressApp , npm thinks you are in the C:\Users\danielk\ project and thus save the express module there.
As for why C:\Users\danielk\ became an npm project, is it either you manually did npm init there before, or you performed your first npm install there, and C:\Users\danielk\node_modules is created, marking it an npm project.
And one more thing, that express is not installed "globally", you do "global" install by npm install express -g (well though for express there is no use to install it globally). Your case is just the package getting "installed in the home directory".

Related

Why I'm getting an error when creating a npm package

I am new with NPM. Below hackerrank question is asking next:
Create the file package.json using npm commands
The name of the app should be npm_package.
Tha start point will be index.js.
The project should have the following elements of dependencecies
Install the latest version of react
Lodash with major version 4 and minor version 17
Redux with Major version 4
Mocha dor testing in Dev
Eslint with major version 6 in Dev
So, I tried these commands but it was marked as fail
mkdir npm_package
cd npm_package
npm init
npm install react --save
npm install lodash#4.17.0 --save
npm install redux#4.0.0 --save
npm install mocha --save-dev
npm install eslint#6.0.0 --save-dev
What I am missing? Below is the result:
grep: /projects/challenge/package.json: No such file or directory
grep: /projects/challenge/package.json: No such file or directory
find: ‘/projects/challenge/node_modules’: No such file or directory
grep: /projects/challenge/package.json: No such file or directory
Test cases executed = 4
PASS = 0 FAIL=4
**challenge/npm_package$ ls**
**node_modules package.json package-lock.json**
To initialize to the default package.json we have to supply the -y flag:
npm init -y

npm link removes child dependencies

I am trying to do local development of an NPM package and test it in a package which depends on it. I'm using NPM (7.5.3) and specifically npm link for this but running into a problem with the chain of dependencies.
The child package has dependencies, these are all added to the parent's node_modules folder when using npm install "git+https://github.com/name/child_package". But when I npm link that module:
cd child_package
npm link
cd ../parent_package
npm link child_package
With the last command run (npm link child_package), all of the dependencies for child_package which were in the node_modules of parent_package are removed. NPM reporting:
removed 60 packages, changed 1 package, and audited 231 packages in 1s
At which point all the compilation in the parent package fails due to the missing deps. It finds the child_package, which is symlinked as expected, but dependency defined in child_package of "gsap" has now been removed.
If I reinstall it using npm install "git+https://github.com/name/child_package" it will add the deps back into the node_modules folder of the parent project.
try to do the following:
cd child_package
npm install
that will install child dependencies to directory of child package
personally I hate npm link and always use npm publish (use version number like 1.0.0-preview.1 for your child package and remove '-preview.Number' when you are done)
This is a behavior introduced in npm V7 + .
The only reasonable "workaround" i have found is to go back to npm 6 (npm install -g npm#6).
Another "workaround" is to npm install --no-save ../../my-local-module but to reflect changes to the local module you will need to delete it from node_modules and reinstall again. Kind of lame....

npm package is not listed in project's package.json but no extraneous error

I see a weird situation in my project.
There are 3 packages not listed in package.json, but installed during the development process.
In my understanding, npm list should show me extraneous error. However, no error displayed. I am wondering how npm decides which package is extraneous or not?
The three packages are async, debug and mime. and I am using the npm 1.4.28
First off, you should update your npm, since 1.4.28 is quite old; latest is 2.4.1.
Secondly, a package is only extraneous if it is not a (dependency, devDependency, optionalDependency) named in package.json or any of its dependencies.
For example, I can create this scenario:
$ mkdir test && cd test
$ echo {} > package.json
$ npm install --save jslint
$ npm install exit
$ npm ls
Now exit is extraneous, even though it is a dependency of jslint, because jslint has its own version of exit under node_modules/jslint/node_modules/exit. Let's get rid of that:
$ rm -rf node_modules/jslint/node_module/exit
$ npm ls
Now exit is no longer extraneous because it is needed to fulfill the dependency of jslint. But if I look in ./node_modules I will see two packages, exit and jslint, only one of which is named in package.json.
Please let me know if I have misunderstood your question.
I am wondering how npm decides which package is extraneous or not?
Installed package that is not in package.json will trigger npm extraneous error.
Try to refresh local node_modules directory.
Install needed packages with --save/--save-dev key.

Clarification of the --save option for npm install

First experiences with node.js/npm. From the npm-install docs I read:
npm install takes 3 exclusive, optional flags which save or update the package version in your main package.json:
--save: Package will appear in your dependencies.
--save-dev: Package will appear in your devDependencies.
--save-optional: Package will appear in your optionalDependencies.
But I can't understand how it works in practice. If, for example, I run the command:
npm install bower --save-dev
I'd expect to find a package.json file in the current directory with devDependencies set to the installed version of bower, instead I find nothing.
Am I doing/expecting something wrong?
Using node v0.10.21, npm 1.3.12 on Ubuntu 12.04 x64
npm won't create package.json for you, but it will create the necessary dependencies for you as long as package.json exists and is legal JSON.
Create it like so
echo {} > package.json
Then, doing npm i --save whatever will add whatever#~x.x.x as a dependency as expected. The file needs to be there, and be JSON, that's it.
npm install only fetches the packages from the registry and puts them in your ./node_modules. It updates your package.json to register this new dependency if you tell it to.
Your package.json has three dependency blocks :
dependencies - these are needed for your app to run.
devDependencies - these are needed for the developer environments for your app (this is how your teammates can get the packages that you recently added to the project. The dependencies listed here are not required on production hosts.)
optionalDependencies - These packages are optional and it is OK if npm cant reolve the package to install. i.e a build failure does not cause npm install to fail (Note however, that your app should handle cases where the package cannot be found.)
Here is the behavior with the different usages of the npm install command:
$ npm install async #Only installs, no change made to package.json
$ npm install async --save #Installs, adds async#version to dependencies block
$ npm install async --save-dev # Installs, adds async#version to the devDependencies block
$ npm install async --save-optional # Installs, adds async#version to the optionalDependencies block

npm installs all modules in /usr/local/lib/node_modules/

I have node.js 0.8.14 installed on Ubuntu 12.10. I created a directory in my home directory with a sub directory node_modules. I want to install some local node modules there but running
npm install myModule
in this directory installs this module in /usr/local/lib/node_modules/ (same behavior as installing the module with the -g flag
There is no node path in .bashrc.
Any idea how I can install local node modules?
After some further research I found the solution.
Running the command npm config ls revealed that the default config global=false (you see the default config with npm config ls -l) was overwritten by global=true in /home/vsdev/.npmrc and /usr/local/etc/npmrc.
Reverting this to global=false solved the issue.
That is odd.
FYI you don't need to create the node_modules directory, npm will do that for you
npm normally just installs to the current directory. Even if the package you are installing is configured to prefer global installation, npm will install it locally unless you explicitly pass the -g parameter.
can you run the following shell commands and confirm npm is really the real npm?
which npm
alias | grep npm
npm install load all in node_modules then it might be version 3 behaviour http://blog.npmjs.org/post/110924823920/npm-weekly-5 or as mentioned by #vsdev so once you make sure it version 3 behaviour and u want to go with it then its fine else follow below
1- uninstall all modules.. into the node_modules folder in your project then execute: npm uninstall *
2- Tell npm to install with legacy bundling for this one install:
npm install --legacy-bundling
A "permanent" alternative:
Set your npm config to always use legacy bundling...
npm set legacy-bundling=true
.. and run as usual:
npm install
*fetching dependencies with legacy bundling will take a lot more time because many several different versions of the same dependencies will be installed.

Resources