Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Optional peer dependency

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?

like image 491
John Smith Avatar asked May 27 '20 16:05

John Smith


People also ask

What is an optional dependency?

Optional dependencies are used when it's not possible (for whatever reason) to split a project into sub-modules. The idea is that some of the dependencies are only used for certain features in the project and will not be needed if that feature isn't used.

What is a peer dependency?

Peer dependencies are a special type of dependency that would only ever come up if you were publishing your own package. Having a peer dependency means that your package needs a dependency that is the same exact dependency as the person installing your package.

What are npm optional dependencies?

Finally, optionalDependencies are dependencies that don't necessarily need to be installed. If a dependency can be used, but you would like npm to proceed if it cannot be found or fails to install, then you may put it in the optionalDependencies object.

What is npm Peer dependency?

Peer Dependencies are used to specify that our package is compatible with a specific version of an npm package. Good examples are Angular and React. To add a Peer Dependency you actually need to manually modify your package.json file.


2 Answers

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! :)

like image 81
tmilar Avatar answered Oct 16 '22 20:10

tmilar


Just a small addition to @tmilar's answer. I'm using the same way to add optional dependancy (and also winston :)) the example is for npm@7 which supports peerDependenciesMeta

"peerDependencies": { 
  "winston": "~3.3.0"
},
"peerDependenciesMeta": {
  "winston": {
    "optional": true
  }
}

for optional dependencies you should expect exceptions on require and check package versions

let winston;
let winstonVersion;
try {
  winston = require('winston')
  winstonVersion = require('winston/package.json').version
} catch (er) {
  winston = null
}

if (isUnsupportedVersion(winstonVersion) ) {
  winston = null
}

// .. then later in your program ..

if (winston) {
  winston.doSomething()
}
like image 28
Anatoli Klamer Avatar answered Oct 16 '22 20:10

Anatoli Klamer