Some things to take into consideration. Let's supose that your npm-calculator also depends on react and your npm-calculator-test also depends on react
you will have react installed in 2 places
Now when you use npm-calculator it will load react from here
npm-calculator/node_modules/react
Instead from here
npm-calculator-test/node_modules/react
because of how modules are resolved. And you may encounter strange errors.
To fix this you will need to tell your bundler to resolve all dependencies from npm-calculator-test/node_modules/** instead of npm-calculator/node_modules/**
Thanks Andrew! This is a useful addition actually!
I would have thought NPM would resolve only one of the dependencies based on it's version checks it does.
This isn't quite true. Npm is smart enough to resolve common versions of packages so if they both depended on the same major version of react, it would be fine.
This is probably a bad example, though, as 99.9% of the time react would be a peer dependency instead (which completely alleviates this problem and gives the version responsibility to the consumer)
Some things to take into consideration. Let's supose that your
npm-calculator
also depends onreact
and yournpm-calculator-test
also depends onreact
you will have
react
installed in 2 placesNow when you use
npm-calculator
it will loadreact
from hereInstead from here
because of how modules are resolved. And you may encounter strange errors.
To fix this you will need to tell your bundler to resolve all dependencies from
npm-calculator-test/node_modules/**
instead ofnpm-calculator/node_modules/**
webpack.js.org/configuration/resol...
webpack.config.js
And now every import of
react
should be loaded from herenpm-calculator-test/node_modules/react
Thanks Andrew! This is a useful addition actually!
I would have thought NPM would resolve only one of the dependencies based on it's version checks it does.
Created a small demo here github.com/iamandrewluca/example-n...
Outputs
This isn't quite true. Npm is smart enough to resolve common versions of packages so if they both depended on the same major version of react, it would be fine.
This is probably a bad example, though, as 99.9% of the time react would be a peer dependency instead (which completely alleviates this problem and gives the version responsibility to the consumer)
I have to mention again, that this is a specific use case when you use
npm link
Unfortunately that's true. NPM is just a package manager, install/uninstall dependencies. Node is the one who resolves dependencies.
nodejs.org/api/modules.html#module...
Smart enough should be the bundler to know what exactly to load.
github.com/iamandrewluca/example-n...
I mentioned it in another comment but in my experience, npmjs.com/package/yalc solves all of these kind of issues. It's so useful
Just use PNPM