node.js npm package.json 特定于操作系统的依赖项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15176082/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 13:43:50  来源:igfitidea点击:

npm package.json OS specific dependency

node.jsnpmnode-gyp

提问by sandeepmistry

Is there a way to specify OS specific dependencies in a npm package.json file?

有没有办法在 npm package.json 文件中指定特定于操作系统的依赖项?

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.

例如,如果用户运行 Linux ,我只想安装 'dbus' ( https://npmjs.org/package/dbus) 作为我的模块的依赖项。我对 Mac 和 Windows 有不同的依赖。

采纳答案by TinyTimZamboni

There's a possible good way of doing this, depending on your setup.

有一种可能的好方法可以做到这一点,具体取决于您的设置。

npm package.json supports an oskey,

npm package.json 支持os键,

and also optionalDependencies

以及可选的依赖项

  • oscan be used to specify which OS a module can be installed on.
  • optionalDependenciesare module dependencies that if they cannot be installed, npm skips them and continues installing.
  • os可用于指定模块可以安装在哪个操作系统上。
  • optionalDependencies是模块依赖项,如果无法安装,npm 会跳过它们并继续安装。

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 ^.^

通过这种方式,您可以让您的模块对每个操作系统都有一个可选的依赖项,并且只会加载/安装有效的模块 ^.^

EDIT: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.

编辑:正如@Sebastien 在下面提到的,这种方法是危险的。对于任何给定的操作系统,您的依赖项中至少有一个是“必需的”,其余的则是“可选的”。将依赖项的所有版本设为可选意味着如果您的安装因正当原因失败,它将悄悄跳过安装,您将丢失真正需要的依赖项。

回答by Nathan Friedly

I think the short answer is no. I can think of a couple of workarounds though - the simplest is to just add everything to package.json regardless of OS, and then require()the correct one at runtime.

我认为简短的回答是否定的。不过,我可以想到几种解决方法 - 最简单的方法是将所有内容添加到 package.json 而不考虑操作系统,然后require()在运行时添加正确的方法。

If that doesn't work for you, you might be able to use an install script to get the result you're going for - https://docs.npmjs.com/misc/scripts

如果这对您不起作用,您可以使用安装脚本来获得您想要的结果 - https://docs.npmjs.com/misc/scripts

I haven't tested this but I think it would work:

我还没有测试过这个,但我认为它会起作用:

Add something like this to your package.json:

将这样的内容添加到您的 package.json 中:

,"scripts": {
  "install": "node install_dependencies.js"
}

And then add a install_dependencies.jsfile that checks the OS and runs the appropriate npm install ...commands.

然后添加一个install_dependencies.js文件来检查操作系统并运行适当的npm install ...命令。

回答by TinyTimZamboni

There's also the bindings-shypmodule:

还有bindings-shyp模块:

https://www.npmjs.com/package/bindings-shyp

https://www.npmjs.com/package/bindings-shyp

Helper module for loading your native module's .node file

This is a helper module for authors of Node.js native addon modules. It is basically the "swiss army knife" of require()ing your native module's .node file.

Throughout the course of Node's native addon history, addons have ended up being compiled in a variety of different places, depending on which build tool and which version of node was used. To make matters worse, now the gyp build tool can produce either a Release or Debug build, each being built into different locations.

This module checks all the possible locations that a native addon would be built at, and returns the first one that loads successfully.

用于加载本机模块的 .node 文件的辅助模块

这是 Node.js 原生插件模块作者的辅助模块。它基本上是 require() 本地模块的 .node 文件的“瑞士军刀”。

在 Node 的原生插件历史的整个过程中,插件最终被编译在各种不同的地方,这取决于使用的构建工具和节点的版本。更糟糕的是,现在 gyp 构建工具可以生成 Release 或 Debug 构建,每个构建到不同的位置。

该模块检查本地插件将被构建的所有可能位置,并返回第一个成功加载的位置。

回答by RzR

Quoting @npm_support at:

引用 @npm_support 在:

https://twitter.com/npm_support/status/968195526989512705

https://twitter.com/npm_support/status/968195526989512705

2/2 If you'd like to avoid installation problems related to dependencies, one route is for you to write a wrapper that's required as a regular dependency, and to make sure that it has optionalDeps(and also ensure that the wrapper verifies you have everything needed to work).

2/2 如果您想避免与依赖项相关的安装问题,一种方法是让您编写一个作为常规依赖项所需的包装器,并确保它具有optionalDeps(并确保包装器验证您拥有一切需要工作)。

But IMHO it looks more like a workaround than solving the problem for real.

但恕我直言,它看起来更像是一种解决方法,而不是真正解决问题。

I can understand that npm wants to preserve portability and avoid to deal with platform specifics, but it has to be done anyway and IMHO doing this at runtime is not optimal (specialty if one wants do optimize code size).

我可以理解 npm 想要保持可移植性并避免处理平台细节,但无论如何都必须完成,恕我直言,在运行时这样做不是最佳的(如果有人想优化代码大小,这是专门的)。

So today I have no optimal solution to share but an open discussion for proposal.

所以今天我没有最佳解决方案可以分享,只能公开讨论提案。

Can't "conditional dependencies" be supported in npm ?

npm 不能支持“条件依赖”吗?

The 1st thing that came to my mind was to to add a "override" section that will change (+add, -remove, =replace) current parsed sections.

我想到的第一件事是添加一个“覆盖”部分,该部分将更改(+add、-remove、=replace)当前解析的部分。

For example:

例如:

dependencies: { "common-stuff": "*" } overrides: { "os: { linux: { dependencies: { "+best-linux-module" } } } }

dependencies: { "common-stuff": "*" } overrides: { "os: { linux: { dependencies: { "+best-linux-module" } } } }

And other option suggested by a developer I know, would be to introduce a provideskeyword, then several modules could provide a same semantic than would be satisfied by resolver (a la debian), but it's generating similar overhead.

我认识的开发人员建议的其他选项是引入一个提供关键字,然后几个模块可以提供与解析器(la debian)满足的语义相同的语义,但它会产生类似的开销。

I am looking for a generic approach not only focused on OS support but also on other flavors of package (depending on engines for instance).

我正在寻找一种通用方法,不仅关注操作系统支持,还关注其他类型的包(例如取决于引擎)。

Do you know any related issue in NPM tracker ? if not I am considering to file a bug to be tracked at:

您知道 NPM 跟踪器中的任何相关问题吗?如果不是,我正在考虑提交一个要跟踪的错误:

https://github.com/npm/npm/issues?q=dependencies+conditional

https://github.com/npm/npm/issues?q=dependencies+conditional

Feedback welcome on this idea.

欢迎反馈这个想法。