Is it possible to specify a runtime dependency of a code generator?

Viewed 92

I am working on an npm package that is used as a CLI to generate code. It makes sense that this project should generally be used as a devDependency.

But the generated code will require a third-party library at runtime. Is there a way to manage this dependency via package.json? I have seen other libraries include a note in the README that defines this dependency, but there is no guarantee that the end user will read this README, so I would like to be able to enforce this.

One thought I had was to use a preinstall script to validate that this third-party library was already installed, otherwise throw an error and don't allow the user to install. But that feels like more of a hack than a solution.

I anticipate someone saying "but won't it be obvious when none of the generated code works?" which is absolutely correct, but ideally this process should fail well before runtime.

I am not sure the specifics really matter here, but my package generates boilerplate TypeScript code for an API project, from an OpenAPI spec, and depends on Express.js at runtime.

1 Answers

This sounds like a peer dependency. You can specify a peer dependency in package.json with the peerDependencies key.

In npm 6, missing peer dependencies will result in a warning.

In npm 7, missing peer dependencies are installed.

Related