最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin3浏览0评论

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?
发布评论

评论列表(0)

  1. 暂无评论