javascript - Is the loading of ESModule asynchronous? - Stack Overflow

I read the documentation of bun two days ago. I am having a hard time understanding this:"The bigg

I read the documentation of bun two days ago. I am having a hard time understanding this:

"The biggest difference between CommonJS and ES Modules is that CommonJS modules are synchronous, while ES Modules are asynchronous. ".

I googled it and found out many people also mentioned this, but no one explained it. Here's some reference:

  • How to dynamically load ESM in CJS
  • CommonJS (cjs) and Modules (esm): Import patibility
  • Interoperability with CommonJS

From my point, the loading of ES modules are synchronous with the static import. For example:

// exporter.mjs
console.log("exporter");
// importer.mjs
import "exporter.mjs"
console.log("importer");

Using this mand node importer.mjs. Well, if the loading of exporter.js is asynchronous, the output would be "importer" printing first and "exporter" following. While the result is "exporter" printing first and "importer" following. enter image description here

This proved that the loading of ES modules are synchronous with the static import, in which case we could import ES modules in CommonJS modules. While the article I mentioned above all say "we can't import ES modules in CommonJS modules, we have to use the dynamic import function" , so did the documentation of nodejs. However, we could import ES modules in CommonJS modules in Bun. This is an example:

// exporter.mjs
export let number = 10;
// importer.cjs
const { number } = require("./exporter.mjs");

console.log(number);

Using this mand bun importer.cjs. We can get the output like this. enter image description here

I am really confused.

I read the documentation of bun two days ago. I am having a hard time understanding this:

"The biggest difference between CommonJS and ES Modules is that CommonJS modules are synchronous, while ES Modules are asynchronous. ".

I googled it and found out many people also mentioned this, but no one explained it. Here's some reference:

  • How to dynamically load ESM in CJS
  • CommonJS (cjs) and Modules (esm): Import patibility
  • Interoperability with CommonJS

From my point, the loading of ES modules are synchronous with the static import. For example:

// exporter.mjs
console.log("exporter");
// importer.mjs
import "exporter.mjs"
console.log("importer");

Using this mand node importer.mjs. Well, if the loading of exporter.js is asynchronous, the output would be "importer" printing first and "exporter" following. While the result is "exporter" printing first and "importer" following. enter image description here

This proved that the loading of ES modules are synchronous with the static import, in which case we could import ES modules in CommonJS modules. While the article I mentioned above all say "we can't import ES modules in CommonJS modules, we have to use the dynamic import function" , so did the documentation of nodejs. However, we could import ES modules in CommonJS modules in Bun. This is an example:

// exporter.mjs
export let number = 10;
// importer.cjs
const { number } = require("./exporter.mjs");

console.log(number);

Using this mand bun importer.cjs. We can get the output like this. enter image description here

I am really confused.

Share Improve this question edited Sep 16, 2023 at 0:40 Devin Johw asked Sep 16, 2023 at 0:36 Devin JohwDevin Johw 1438 bronze badges 7
  • 1 Think in terms of multiple imports. You can have 20 imports, and in Node, as legacy CJS modules, those will all load one by one, in order. In modern JS, as ESM, all 20 will load concurrently without any guarantee about what order they actually load in (which is fine, bcause that's not something you should need to rely on anyway). – Mike 'Pomax' Kamermans Commented Sep 16, 2023 at 0:41
  • If the module is loading asynchronously, why node print the exporter first?

    发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744405050a4572588.html

相关推荐

  • javascript - Is the loading of ESModule asynchronous? - Stack Overflow

    I read the documentation of bun two days ago. I am having a hard time understanding this:"The bigg

    7天前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信