Skip to content

Commit

Permalink
doc: add examples for implementing ESM
Browse files Browse the repository at this point in the history
Fixes: #28060

PR-URL: #33168
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Bradley Farias <[email protected]>
  • Loading branch information
rosaxxny authored and codebytere committed May 16, 2020
1 parent 354ff4f commit 0ffa040
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions doc/api/esm.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,27 @@ 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
function addTwo(num) {
return num + 2;
}

export { addTwo };
```

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

```js
// app.js
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

0 comments on commit 0ffa040

Please sign in to comment.