【问题标题】:Making a dependency's dependencies available with Yarn 2使用 Yarn 2 使依赖项的依赖项可用
【发布时间】:2020-06-10 07:51:16
【问题描述】:

我正在开发一个框架,我希望框架包的依赖项可用于使用它的应用程序。依赖不是消费者直接使用的,而是框架提供的文件使用的。

使用 npm,它可以工作,但使用 Yarn 2 我会遇到这样的错误

Error: Your application tried to access @snowpack/plugin-dotenv, but it isn't declared in your dependencies; this makes the require call ambiguous and unsound.

显然,我可以将依赖项添加到使用应用程序的 package.json 中,但这需要在依赖项更改时手动编辑 package.json。这违背了具有无模糊升级路径的框架的想法。此外,依赖项适用于框架提供的文件。它们不应受到下游代码的干扰。

是否可以在下游共享依赖项?

注意:我正在使用工作区。我不知道这是否与问题有关。

【问题讨论】:

  • 切换到 pnpm 及其“--shamefully-hoist”选项。

标签: dependencies yarnpkg yarnpkg-v2


【解决方案1】:

您可以使用createRequire 来实现。

例如,如果您的应用程序依赖于micromatch@4.0.2,并且您希望在应用程序代码中要求其子依赖关系picomatch 而不声明它,您可以通过以下代码代表micromatch 执行此操作:

const {createRequire} = require(`module`);
const requireDependency = createRequire(require.resolve(`micromatch`));
requireDependency(`picomatch`);

这种方法适用于所有包管理器和安装策略,包括 Yarn v2 pnppnpmnpm

【讨论】:

    猜你喜欢
    • 2018-04-13
    • 1970-01-01
    • 2015-11-05
    • 2021-11-18
    • 2014-11-19
    • 2021-03-22
    • 2018-07-19
    • 2017-06-03
    • 2018-08-29
    相关资源
    最近更新 更多