From 0ffa0402a5dfe313fd6a3d683c9e3d1a50142938 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 PR-URL: https://github.com/nodejs/node/pull/33168 Reviewed-By: Anna Henningsen Reviewed-By: Bradley Farias --- doc/api/esm.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/doc/api/esm.md b/doc/api/esm.md index 30534a5cdaa49b..f3a34e7d463e41 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][].