From bf5dd8cb0cb0059807b4d18e037b2326e02c22ae Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 30 Apr 2020 12:32:53 -0400 Subject: [PATCH] doc: add examples for implementing ESM Fixes: https://github.com/nodejs/node/issues/28060 --- doc/api/esm.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/doc/api/esm.md b/doc/api/esm.md index 49c467effbc3cd..fb86b12ea5cc8a 100644 --- a/doc/api/esm.md +++ b/doc/api/esm.md @@ -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'; +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'; +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][].