【问题标题】:Run Nodejs program that ignores parent node_modules运行忽略父 node_modules 的 Nodejs 程序
【发布时间】:2020-02-09 10:32:39
【问题描述】:

我有一个库,我想在发布到 npm 之前通过在本地进行完整安装来测试它。我要测试的一件事是所有依赖项是否都是项目的一部分。由于 nodejs 构建其模块系统的方式,(a nodejs module is searched for at every directory level) 我没有一个好的方法来确保我需要的模块来自当前文件夹的 node_modules,而不是父文件夹的 node_modules。

这是项目结构:

library-folder/
  package.json
  node_modules/
  testing/integration/
    package.json
    node_modules/
    test-install.js

我想知道是否有办法告诉 nodejs 只需要当前目录中的模块,而忽略任何父目录。

【问题讨论】:

标签: node.js node-modules


【解决方案1】:

我没有很好的方法来确保我需要的模块来自当前文件夹的 node_modules,而不是父文件夹的 node_modules。

为此,您可以使用require.resolve 并检查它返回的路径。

【讨论】:

  • 我不相信这会根据他们的文档起作用:“这些路径中的每一个都用作模块解析算法的起点,这意味着从这个位置检查 node_modules 层次结构。”我无法查看此自动取款机,但我会在今天晚些时候查看
  • @andykais 看起来你已经阅读了options.paths 字段的描述。方法说明写在要点后面:Use the internal require() machinery to look up the location of a module, but rather than loading the module, just return the resolved filename.
  • 谢谢你,你说得对,这会让我成功,但我认为我试图解决的问题是不可能的。假设我想确定我真正的部门不是 devDeps。即使我知道testing/integration/test-install.js 正在使用integration/node_modules 文件夹,我的库的任何依赖项仍然会在integration/node_modules 和library-folder/node_modules 中查找,因为每个需要位置只是一个起点。我认为我真正的解决方案是在同级文件夹中进行测试。
【解决方案2】:

我通过在孩子的根项目文件夹中创建一个文件解决了这个问题。

文件名应该这样命名:.env

内容:SKIP_PREFLIGHT_CHECK=true

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-18
    • 2018-06-22
    • 1970-01-01
    • 2010-11-05
    • 2016-07-15
    • 2018-06-11
    相关资源
    最近更新 更多