You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/content/guides/ecma-script-modules.mdx
+10-8Lines changed: 10 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -125,12 +125,14 @@ W> Avoid top-level await in your entry point when targeting the **browser**. It
125
125
```js
126
126
// user.js (async ESM module)
127
127
constresponse=awaitfetch("/api/user");
128
+
128
129
exportconstuser=awaitresponse.json();
129
130
```
130
131
131
132
```js
132
133
// index.js - importing an async module works as expected
133
134
import { user } from"./user.js";
135
+
134
136
console.log(user.name);
135
137
```
136
138
@@ -139,11 +141,11 @@ console.log(user.name);
139
141
Imports in ESM are resolved more strictly. Relative requests must include a file extension (e.g. `*.js` or `*.mjs`) following the Node.js convention when the file is flagged as ESM:
140
142
141
143
```js
144
+
// will fail - missing extension
145
+
import { helperasmissingExt } from"./utils";
146
+
142
147
// correct in ESM
143
148
import { helper } from"./utils.js";
144
-
145
-
// will fail - missing extension
146
-
import { helper } from"./utils";
147
149
```
148
150
149
151
T> Requests to packages e.g. `import"lodash"` are still supported.
@@ -174,15 +176,15 @@ When importing from a CommonJS module inside ESM, only the `default` export
174
176
is available (the entire `module.exports` object):
175
177
176
178
```js
177
-
// cjs-module.js (CommonJS)
178
-
module.exports= { foo:1, bar:2 };
179
-
180
179
// esm-consumer.js (ESM)
181
180
importcjsfrom"./cjs-module.js";
182
-
console.log(cjs.foo); // works - cjs is the whole exports object
0 commit comments