Skip to content

Commit 0ffa040

Browse files
rosaxxnycodebytere
authored andcommitted
doc: add examples for implementing ESM
Fixes: #28060 PR-URL: #33168 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Bradley Farias <[email protected]>
1 parent 354ff4f commit 0ffa040

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

doc/api/esm.md

+21
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,27 @@ ECMAScript modules are [the official standard format][] to package JavaScript
1313
code for reuse. Modules are defined using a variety of [`import`][] and
1414
[`export`][] statements.
1515

16+
The following example of an ES module exports a function:
17+
18+
```js
19+
// addTwo.js
20+
function addTwo(num) {
21+
return num + 2;
22+
}
23+
24+
export { addTwo };
25+
```
26+
27+
The following example of an ES module imports the function from `addTwo.js`:
28+
29+
```js
30+
// app.js
31+
import { addTwo } from './addTwo.js';
32+
33+
// Prints: 6
34+
console.log(addTwo(4));
35+
```
36+
1637
Node.js fully supports ECMAScript modules as they are currently specified and
1738
provides limited interoperability between them and the existing module format,
1839
[CommonJS][].

0 commit comments

Comments
 (0)