diff options
author | unknown <rosaxny@gmail.com> | 2020-04-30 12:32:53 -0400 |
---|---|---|
committer | Anna Henningsen <anna@addaleax.net> | 2020-05-14 19:57:04 +0200 |
commit | 47804933012841f2dc90626bdcc161adf34569a5 (patch) | |
tree | 214028a5d4a9f3cee079f523a7fc74598c8bfb05 | |
parent | 5ae5262f448295e314393dad4c491259793cea3f (diff) | |
download | node-new-47804933012841f2dc90626bdcc161adf34569a5.tar.gz |
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>
-rw-r--r-- | doc/api/esm.md | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/doc/api/esm.md b/doc/api/esm.md index 30534a5cda..f3a34e7d46 100644 --- a/doc/api/esm.md +++ b/doc/api/esm.md @@ -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][]. |