How npm install works
When I run npm install inside an app folder, however, it appears to install everything locally regardless of where else it may exist upstream. Is that the correct behavior? (It's possible there's another reason, like bad version language in my package.json). If this IS the correct behavior, is there a way for me to have npm install behave like the above?
Yes, that is what npm install does. In node.js code, the require
algorithm has a particular sequence of places it looks, including walking up the filesystem. However, npm install
doesn't do that. It just installs in place. The algorithms it uses are all constrained to just a single node_modules
directory under your current directory and it won't touch anything above that (except for with -g
).
It's not a big deal to widely replicate the modules inside every app, but it feels messy and prevents me from make small improvements to the common modules and not having to update every old package.json file. Of course, this could be a good thing...
Yeah basically you're doing it wrong. The regular workflow scales well to the Internet. For your use case it creates some extra tedious work, but you can also just use semantic versioning as intended and specify "mylib": "^1.0.0"
in your package.json for your apps and be OK with automatically getting newer versions next time you npm install
.
Chris Wilson
Author of RaphaelJS: Graphics and Visualization on the Web Director of Data Journalism, Time.com https://mechanicalscribe.com/ Twitter: @chriswilsondc GitHub: @wilson428 LinkedIn
Updated on September 21, 2020Comments
-
Chris Wilson over 3 years
I use Node.js (via browserify) for each of my web apps, all of which have some dependencies in common and others specific to themselves. Each of these apps has a
package.json
file that specifies which versions of which modules it needs.Right now, I have a
/node_modules
directory in the parent folder of my apps for modules that they all need to reference, and then I put app-specific modules in anode_modules
folder in that app's directory. This works fine in the short term, since myrequire()
statements are able to keep looking upward in the file structure until they find thenode_modules
directory with the correct app in it.Where this gets tricky is when I want to go back to an old project and run
npm install
to make sure it can still find all the dependencies it needs. (Who knows what funny-business has occurred since then at the parent directory level.) I was under the impression thatnpm install
did this:- for each module listed in
package.json
, first check if it's present, moving up the directory the same wayrequire
does. If it's not, install it to the localnode_modules
directory (creating that directory if necessary).
When I run
npm install
inside an app folder, however, it appears to install everything locally regardless of where else it may exist upstream. Is that the correct behavior? (It's possible there's another reason, like bad version language in mypackage.json
). If this IS the correct behavior, is there a way for me to havenpm install
behave like the above?It's not a big deal to widely replicate the modules inside every app, but it feels messy and prevents me from make small improvements to the common modules and not having to update every old
package.json
file. Of course, this could be a good thing... - for each module listed in
-
Alejandro B. over 7 yearsI feel a little more info on what ^1.0.0 does would have been great for the people that come to this question when googling for help on npm.
-
Piyush Balapure over 7 yearsAs @AlejoBrz said, if anyone wants more info on versioning, visit docs.npmjs.com/getting-started/semantic-versioning
-
Ciasto piekarz almost 6 yearsI am a python developer, I believe its packaging dependencies like a virtualenv self contained.