I'm developing a module A. The user can, optionally, inject a Winston logger to my module, therefore winston
is its peer dependency.
Whenever I install my module A in another module where I don't want to log stuff (therefore I don't include Winston) and try to tsc
it, Typescript yells:
Cannot find module 'winston' or its corresponding type declarations.
How do I go about it?
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! :)