需要目录而不是JS文件

发布于 2025-01-28 23:40:12 字数 228 浏览 3 评论 0原文

我在index.js文件中遇到了这样的件代码:

exports = module.exports = require("./src")

不确定exports = module.exports bit是什么。 并且不确定导入整个目录/src的含义。

I come across a piece code like this in an index.js file:

exports = module.exports = require("./src")

Not sure what the exports = module.exports bit means.
And not sure what it means to import an entire directory /src.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

撩发小公举 2025-02-04 23:40:12

tl; dr

这些是您问题的解决方案,从第一到第二(按顺序)。

  1. 最有可能将变量导出设置为module.exports
  2. 在文件夹中导入默认文件(例如index.js),是否指定。

1。 enfort变量是什么定义

?进行更正:

评论#1

您对1的解释是不正确的。如果它们是如图所示的单独语句,导出将等于module.exportsmodule.exports的上一个值等于requience requience等于>。将它们链接在一起,将Exportsmodule.exports设置为requient requient返回的值。目前尚不清楚为什么有人会这样做,但是他们这样做了。

评论#2

我们应该非常谨慎地解释在语句中可能是什么结果,例如其取决于变量是否导出模块(使用/没有属性导出)已经存在,以及我们是否正在使用严格模式。还有许多其他帖子扩展了“链接分配”。

注意:我在这条线上也很混乱,所以这是我受过良好教育的猜测。请随时在评论中纠正我,或通过编辑我的帖子。

注意:此解决方案不正确。请参阅 dave Meehan 对此解决方案的纠正的评论。

这行很可能也可以是这样。像这样分为多行。

exports = module.exports;
module.exports = require("./src");

基本上,它只是制作一个称为enfort的变量,然后将其设置为module.exports

也可以使用破坏对象来完成同样的事情。

const { exports } = module;

这将仅从模块中获取导出键,然后制作一个称为exports的新变量。


2。您如何需要一个文件夹?

从技术上讲,节点不会导入整个文件夹。基本上,它正在检查目录中的默认文件。

Node.js将检查文件的情况有两种情况。

  1. 如果main属性是在package.json中定义的,它将在/src目录中查找该文件。

      {
      “ main”:“ app.js”
    }
     

    如果package.json是这样,那么它将搜索/src/app.js

  2. 如果main属性未在package.json中定义,则默认情况下会查找名为index.js的文件。 。因此,它将导入/src/index.js


摘要

总而言之,这些是您问题的解决方案。

  1. 最有可能将变量导出设置为module.exports
  2. 在文件夹中导入默认文件(例如index.js),是否指定。

这应该有助于清除您的困惑。

TL;DR

These are solutions for your questions, from first to second (in order).

  1. Most likely setting the variable exports to module.exports.
  2. Importing a default file in the folder (e.g. index.js) if specified or not.

1. What does the exports variable define?

From Dave Meehan, a few corrections made:

Comment #1

Your explanation of 1 is incorrect. If they were separate statements as illustrated, exports would equal the previous value of module.exports, and module.exports would equal the return value of require. Chaining them together sets both exports and module.exports to the value returned by require. It's not clear why someone would want to do that, but they did.

Comment #2

We should be very careful in interpreting what the result might be in such a statement as its dependent on whether the variables export and module (with/without property exports) already exists, and whether we are using strict mode. There's a number of other SO posts that expand on "chaining assignment".

Note: I am also mildly confused on this line, so this is my educated guess. Feel free to correct me in the comments, or by editing my post.

Note: This solution is incorrect. Please see Dave Meehan's comments on corrections for this solution.

This line most likely can be also spread into multiple lines, like so.

exports = module.exports;
module.exports = require("./src");

Basically, it is just making a variable called exports, and setting that to module.exports.

This same thing can be done using object destructing too.

const { exports } = module;

This will simply get the exports key from module, and make a new variable called exports.


2. How do you require a folder?

Technically, Node isn't importing an entire folder. Basically, it is checking for a default file in the directory.

There are two cases in how Node.js will check for a file.

  1. If the main property is defined in package.json, it will look for that file in the /src directory.

    {
      "main": "app.js"
    }
    

    If the package.json, was like that, for instance, then it would search for /src/app.js.

  2. If the main property isn't defined in package.json, then it will by default look for a file named index.js. So, it would import /src/index.js.


Summary

In summary, these are the solutions to your questions.

  1. Most likely setting the variable exports to module.exports.
  2. Importing a default file in the folder (e.g. index.js) if specified or not.

This should help clear your confusion.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文