mirror of
https://github.com/zebrajr/node.git
synced 2026-01-15 12:15:26 +00:00
doc: add examples for implementing ESM
Fixes: https://github.com/nodejs/node/issues/28060 PR-URL: https://github.com/nodejs/node/pull/33168 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Bradley Farias <bradley.meck@gmail.com>
This commit is contained in:
@@ -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][].
|
||||
|
||||
Reference in New Issue
Block a user