Since NPM v7.x, you can use the peerDependenciesMeta
package.json config, which allows exactly that option.
For example, in your "Module A" package.json
:
"peerDependencies": {
"winston": "> 1.0.0 <= 1.2.10",
"foo": "~2.3.0"
},
"peerDependenciesMeta": {
"winston": {
"optional": true
}
}
In this case, when installing Module A as a dependency of another project, it will allow installing winston
dependency version in the semver range specified > 1.0.0 <= 1.2.10
, but if it's not present at all, you won't get errors, so it will be allowed as well.
Note that following this example, foo
dependency would be still required because it's not marked as optional
.
Extra tip: you can check and test ranges on available NPM packages using this utility https://semver.npmjs.com/, it helped me as well.
PS. this is my first answer on SO! :)