Skip to content

Commit d7c7023

Browse files
GrosSacASactargos
authored andcommitted
doc: add example on how to create __filename, __dirname for esm
PR-URL: #28282 Fixes: #28114 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Franziska Hinkelmann <[email protected]> Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Trivikram Kamat <[email protected]> Reviewed-By: Guy Bedford <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Rich Trott <[email protected]>
1 parent d34c256 commit d7c7023

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

doc/api/esm.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,16 @@ These CommonJS variables are not available in ES modules.
278278

279279
`require` can be imported into an ES module using [`module.createRequire()`][].
280280

281-
An equivalent for `__filename` and `__dirname` is [`import.meta.url`][].
281+
An equivalent for variable `__filename` and `__dirname` can be created inside
282+
each file with [`import.meta.url`][].
283+
284+
```js
285+
import { fileURLToPath } from 'url';
286+
import { dirname } from 'path';
287+
288+
const __filename = fileURLToPath(import.meta.url);
289+
const __dirname = dirname(__filename);
290+
```
282291
283292
### No <code>require.extensions</code>
284293

0 commit comments

Comments
 (0)