[英]What is the difference between javascript require("file") vs require("file.js")?
I have two files.我有两个文件。
In import I do:在进口我做:
const export = require('export')
This will successful import the module from export.js.这将成功从 export.js 导入模块。 When I do
require('export.js')
(with file ending) it is also working.当我确实
require('export.js')
(文件结尾)时,它也在工作。
So I was wondering what is the difference there?所以我想知道那里有什么区别? Is without slower?
没有更慢?
Node.js automatically resolves some extensions. Node.js 自动解析部分扩展名。 For example: .js, .json, etc. No, it works the same with or without the extension.
例如:.js、.json 等。不,不管有没有扩展名,它的工作原理都是一样的。
The difference between import/export
and require
is that require
uses commonjs
under the hood. import/export
和require
之间的区别在于require
在底层使用commonjs
。 While the import
was introduced in ES6.而
import
是在 ES6 中引入的。
Even if you use ES6 import
it's still transpiled into the commonjs
.即使您使用 ES6
import
,它仍然会被转换为commonjs
。 But using ES6 is recommended because with the time, it will be widely supported.但建议使用 ES6,因为随着时间的推移,它会得到广泛支持。
Performance-wise the difference will be negligible because the module files are evaluated only once but you will have to benchmark it, if you are very keen on the performance.在性能方面,差异可以忽略不计,因为模块文件只评估一次,但如果您非常关注性能,则必须对其进行基准测试。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.