From d7c7023503bffa31f31988b2c7826ebe10866770 Mon Sep 17 00:00:00 2001 From: Walle Cyril Date: Wed, 19 Jun 2019 01:21:23 +0200 Subject: [PATCH] doc: add example on how to create __filename, __dirname for esm MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR-URL: https://github.com/nodejs/node/pull/28282 Fixes: https://github.com/nodejs/node/issues/28114 Reviewed-By: Anna Henningsen Reviewed-By: Franziska Hinkelmann Reviewed-By: Michaƫl Zasso Reviewed-By: Luigi Pinca Reviewed-By: Ruben Bridgewater Reviewed-By: Trivikram Kamat Reviewed-By: Guy Bedford Reviewed-By: James M Snell Reviewed-By: Rich Trott --- doc/api/esm.md | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/doc/api/esm.md b/doc/api/esm.md index c4edc523571336..8b7b632f1cdad7 100644 --- a/doc/api/esm.md +++ b/doc/api/esm.md @@ -278,7 +278,16 @@ These CommonJS variables are not available in ES modules. `require` can be imported into an ES module using [`module.createRequire()`][]. -An equivalent for `__filename` and `__dirname` is [`import.meta.url`][]. +An equivalent for variable `__filename` and `__dirname` can be created inside +each file with [`import.meta.url`][]. + +```js +import { fileURLToPath } from 'url'; +import { dirname } from 'path'; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = dirname(__filename); +``` ### No require.extensions