需要目录而不是JS文件
我在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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
tl; dr
这些是您问题的解决方案,从第一到第二(按顺序)。
导出
设置为module.exports
。index.js
),是否指定。1。
enfort
变量是什么定义?进行更正:
评论#1
评论#2
注意:我在这条线上也很混乱,所以这是我受过良好教育的猜测。请随时在评论中纠正我,或通过编辑我的帖子。
注意:此解决方案不正确。请参阅 dave Meehan 对此解决方案的纠正的评论。
这行很可能也可以是这样。像这样分为多行。
基本上,它只是制作一个称为
enfort
的变量,然后将其设置为module.exports
。也可以使用破坏对象来完成同样的事情。
这将仅从
模块
中获取导出
键,然后制作一个称为exports
的新变量。2。您如何
需要
一个文件夹?从技术上讲,节点不会导入整个文件夹。基本上,它正在检查目录中的默认文件。
Node.js将检查文件的情况有两种情况。
如果
main
属性是在package.json
中定义的,它将在/src
目录中查找该文件。如果
package.json
是这样,那么它将搜索/src/app.js
。。
如果
main
属性未在package.json
中定义,则默认情况下会查找名为index.js
的文件。 。因此,它将导入/src/index.js
。摘要
总而言之,这些是您问题的解决方案。
导出
设置为module.exports
。index.js
),是否指定。这应该有助于清除您的困惑。
TL;DR
These are solutions for your questions, from first to second (in order).
exports
tomodule.exports
.index.js
) if specified or not.1. What does the
exports
variable define?From Dave Meehan, a few corrections made:
Comment #1
Comment #2
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.
Basically, it is just making a variable called
exports
, and setting that tomodule.exports
.This same thing can be done using object destructing too.
This will simply get the
exports
key frommodule
, and make a new variable calledexports
.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.
If the
main
property is defined inpackage.json
, it will look for that file in the/src
directory.If the
package.json
, was like that, for instance, then it would search for/src/app.js
.If the
main
property isn't defined inpackage.json
, then it will by default look for a file namedindex.js
. So, it would import/src/index.js
.Summary
In summary, these are the solutions to your questions.
exports
tomodule.exports
.index.js
) if specified or not.This should help clear your confusion.