Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

doc: add examples for implementing ESM #33168

Closed
wants to merge 3 commits into from
Closed
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 23 additions & 0 deletions doc/api/esm.md
Expand Up @@ -13,6 +13,29 @@ ECMAScript modules are [the official standard format][] to package JavaScript
code for reuse. Modules are defined using a variety of [`import`][] and
[`export`][] statements.

The following example of an ES module exports a function:

```js
// addTwo.js
'use strict';
addaleax marked this conversation as resolved.
Show resolved Hide resolved
function addTwo(num) {
return num + 2;
}

export { addTwo };
```

The following example of an ES module imports the function from `addTwo.js`:

```js
// app.js
'use strict';
addaleax marked this conversation as resolved.
Show resolved Hide resolved
import { addTwo } from './addTwo.js';

// Prints: 6
console.log(addTwo(4));
```

Node.js fully supports ECMAScript modules as they are currently specified and
provides limited interoperability between them and the existing module format,
[CommonJS][].
Expand Down