Reputation: 275
I have the following question. Currently I want to build a NPM module which this expected behaviour:
Once I've installed it, all its dependencies would work perfectly in the project only import them.
First of all, what I have is this:
I have "my-module" project as the dependencies modules I want with that specific versions.
On the other hand I have a project which imports it as we can see.
Inside my project I want to write, for example, the following code and finally run it:
import {Calendar} from 'primereact/calendar';
How could I deal with this trouble?
Thanks in advance!
EDIT: My project's package.json is this
{
"name": "my-project",
"version": "0.0.0",
"description": "",
"main": "index.js",
"scripts": {
"dev": "next src",
"build": "next build src",
"start": "next start src"
},
"license": "ISC",
"dependencies": {
"next": "^9.0.3",
"react": "^16.8.6",
"react-dom": "^16.8.6",
"@ascope/my-module": "file://../my-module"
}
}
Also my-module's package.json:
{
"name": "my-module",
"version": "0.0.1",
"main": "index.js",
"scripts": {
"test": "exit 1"
},
"license": "ISC",
"bundledDependencies": [
"primereact",
"rxjs"
],
"dependencies": {
"primereact": "^3.1.8",
"rxjs": "^6.5.2"
},
"peerDependencies": {
"primereact": "^3.1.8",
"rxjs": "^6.5.2"
}
}
Upvotes: 1
Views: 9474
Reputation: 641
In node.js, it is recommended that you declare those dependencies specifically as a part of your app.
Require dependency of another dependency in node modules
Upvotes: 0
Reputation: 14423
Both packages will need the primereact
dependency, when you do an install
on the top level package it will move the primereact
dependency up, assuming there's a version that can satisfy both dependencies.
Have a look at NPM install algorithm
For this package{dep} structure: A{B,C}, B{C}, C{D}, this algorithm produces:
A
+-- B
+-- C
+-- D
That is, the dependency from B to C is satisfied by the fact that A already caused C to be installed at a higher level. D is still installed at the top level because nothing conflicts with it.
For A{B,C}, B{C,D@1}, C{D@2}, this algorithm produces:
A
+-- B
+-- C
`-- D@2
+-- D@1
Because B’s D@1 will be installed in the top level, C now has to install D@2 privately for itself. This algorithm is deterministic, but different trees may be produced if two dependencies are requested for installation in a different order.
You can also run npm dedupe
Upvotes: 1