From 48f20c3d5736cf155438b6ebda23e2fc6f2c7149 Mon Sep 17 00:00:00 2001 From: Walle Cyril Date: Wed, 19 Jun 2019 01:21:23 +0200 Subject: [PATCH 1/4] doc: esm add example how to create __filename, __dirname Fixes: https://github.com/nodejs/node/issues/28114 --- doc/api/esm.md | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/doc/api/esm.md b/doc/api/esm.md index c4edc523571336..ba80974193fc63 100644 --- a/doc/api/esm.md +++ b/doc/api/esm.md @@ -278,7 +278,15 @@ 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 { fileURLToPath } from 'url'; +import { dirname } from 'path'; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = dirname(__filename); + ### No require.extensions From d34f7a541831fea9c2b94235c53c6804aa9aa2a2 Mon Sep 17 00:00:00 2001 From: Walle Cyril Date: Wed, 19 Jun 2019 08:43:32 +0200 Subject: [PATCH 2/4] doc: esm use backtick notation foc code --- doc/api/esm.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/api/esm.md b/doc/api/esm.md index ba80974193fc63..921feee68db416 100644 --- a/doc/api/esm.md +++ b/doc/api/esm.md @@ -280,13 +280,13 @@ These CommonJS variables are not available in ES modules. An equivalent for variable `__filename` and `__dirname` can be created inside each file with - +```js import { fileURLToPath } from 'url'; import { dirname } from 'path'; const __filename = fileURLToPath(import.meta.url); const __dirname = dirname(__filename); - +``` ### No require.extensions From 6d302ee860059e0e0a05ec5343608c6354a08d03 Mon Sep 17 00:00:00 2001 From: Walle Cyril Date: Wed, 19 Jun 2019 20:40:31 +0200 Subject: [PATCH 3/4] doc: esm respect the 80 character limit --- doc/api/esm.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/doc/api/esm.md b/doc/api/esm.md index 921feee68db416..a985259c847aa9 100644 --- a/doc/api/esm.md +++ b/doc/api/esm.md @@ -278,7 +278,8 @@ These CommonJS variables are not available in ES modules. `require` can be imported into an ES module using [`module.createRequire()`][]. -An equivalent for variable `__filename` and `__dirname` can be created inside each file with +An equivalent for variable `__filename` and `__dirname` can be created inside +each file. ```js import { fileURLToPath } from 'url'; From 9aee4e01b853f409c3c7775d3fac50a97b34082e Mon Sep 17 00:00:00 2001 From: Walle Cyril Date: Wed, 19 Jun 2019 20:44:00 +0200 Subject: [PATCH 4/4] doc: esm keep the import.meta.url reference --- doc/api/esm.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/api/esm.md b/doc/api/esm.md index a985259c847aa9..8b7b632f1cdad7 100644 --- a/doc/api/esm.md +++ b/doc/api/esm.md @@ -279,7 +279,7 @@ These CommonJS variables are not available in ES modules. `require` can be imported into an ES module using [`module.createRequire()`][]. An equivalent for variable `__filename` and `__dirname` can be created inside -each file. +each file with [`import.meta.url`][]. ```js import { fileURLToPath } from 'url';