Can you import a `devDependency` in your code?

让人想犯罪 __ 提交于 2019-12-10 23:43:48

问题


Mobx DevTool's README guides you to install it as dev dependency, and then import it into your code. That seems like a problem to me, because devDependencies, as explained by this SO answer, are:

... used for the build process, tools that help you manage how the end code will end up, third party test modules, (ex. webpack stuff)

If that's true, then is it a correct deduction that you shouldn't import a devDependency into your code?


回答1:


It depends on how the code that will import the dependency will be used. If it will only be used in a development context, then yes, it should be a devDependency and it's fine for the code to use it.

If the code that uses it is going to be used by a consumer of your package/module/library, then it is a direct dependency and should be annotated as such.

It's kind of how you view the intention of the source file that consumes the dependency. If I install with npm install your-cool-package, I don't want, and shouldn't need the devDependencies installed because I'm likely not going to be building your module from source, benchmarking, or testing. I'm just going to consume it.

If I need the dependency to consume your module, then it isn't a devDependencies, it is just a straight up dependencies (or maybe a peerDependencies).


Ask yourself this: When I use your module, for it to function, does the dependency need to be there for it to function? If it does, it's a dependency. If not, it's a dev dependency.

If you author a plugin for something, it depends on the thing it plugs in to. If you author a plugin that helps people develop things, the plugin still depends on what it plugs in to, but the person installing your plugin would consider your plugin a development dependency because they don't need it when they aren't developing. However, your plugin still depends directly on the thing that it plugs in to.

It sounds like you are writing a plugin (let's call it cool-plugin) for a module (Mobx). I don't use Mobx, but it sounds like a development tool. Since it sounds like cool-plugin needs Mobx in order to do anything Mobx is a dependency of cool-plugin, but a consumer would consider cool-plugin a devDependencies of their own hypothetical consumer-module because they don't need it in production.

Because of that, you should consider Mobx a dependencies because your module doesn't make sense without it.


There is a case to be made that you should actually consider it a peerDependencies because consumer-module probably doesn't want you to install your own version of Mobx but instead wants you to interface with the one they should already be using.



来源:https://stackoverflow.com/questions/55068767/can-you-import-a-devdependency-in-your-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!