【问题标题】:Where is require() searching for a index.js in node js?require() 在哪里搜索节点 js 中的 index.js?
【发布时间】:2020-10-27 16:11:07
【问题描述】:

我正在尝试了解 nodejs 项目中文件夹的层次结构。 我知道如果您在 node_modules 上的 foo 文件夹中有一个 index.js 并且您调用 require('foo'),它将返回 index.js。好的。

我的问题是:当我(例如)从 npm 下载 'underscore' 包时,它会在 node_modules 内创建一个下划线文件夹,其中包含一些 js 文件. index.js 位于 undersocre 文件夹内的另一个文件夹中,称为模块(有 index.jsindex-all.js)。 Index.js 不应该在那个 'modules' 文件夹之外?

【问题讨论】:

    标签: javascript node.js npm package


    【解决方案1】:

    require() 在 nodejs 中的整个加载逻辑在此处描述:https://nodejs.org/api/modules.html#modules_all_together

    在下划线的情况下,有一个package.json 文件包含一个main 入口点,告诉它运行underscore.js 作为模块的入口点(当您使用require() 加载它时)。

    【讨论】:

      猜你喜欢
      • 2020-12-03
      • 2014-08-27
      • 2016-08-10
      • 2014-09-15
      • 2019-11-03
      • 1970-01-01
      • 1970-01-01
      • 2017-02-17
      • 2022-11-22
      相关资源
      最近更新 更多