npm package.json 特定于操作系统的依赖项 [英] npm package.json OS specific dependency
问题描述
有没有办法在 npm package.json 文件中指定操作系统特定的依赖项?
Is there a way to specify OS specific dependencies in a npm package.json file?
例如,我只想安装dbus"(https://npmjs.org/package/dbus)如果用户运行 Linux,则作为我的模块的依赖项.我对 Mac 和 Windows 的依赖不同.
For example, I would only want to install 'dbus' (https://npmjs.org/package/dbus) as a dependency for my module if the user is running Linux. I would have a different dependency for Mac and Windows.
推荐答案
根据您的设置,有一种可能的好方法可以做到这一点.
There's a possible good way of doing this, depending on your setup.
npm package.json 支持 os 键,
npm package.json supports an os key,
os
可用于指定模块可以安装在哪个操作系统上.optionalDependencies
是模块依赖项,如果无法安装,npm 会跳过它们并继续安装.
os
can be used to specify which OS a module can be installed on.optionalDependencies
are module dependencies that if they cannot be installed, npm skips them and continues installing.
通过这种方式,您可以让您的模块对每个操作系统都有一个可选的依赖项,并且只会加载/安装有效的模块 ^.^
In this way you can have your module have an optional dependency for each OS, and only the one which works will be loaded/installed ^.^
正如@Sebastien 在下面提到的,这种方法很危险.对于任何给定的操作系统,您的依赖项中至少有一个是必需的",其余的则是可选的".将依赖项的所有版本设为可选意味着,如果您的安装因正当原因失败,它会默默地跳过安装,您将丢失真正需要的依赖项.
As @Sebastien mentions below, this approach is dangerous. For any given OS, at least one of your dependencies is "required" and the rest "optional". Making all versions of the dependency optional means that if your installation fails for a legitimate reason, it will silently skip installation and you will be missing a dependency you really need.
这篇关于npm package.json 特定于操作系统的依赖项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!