You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Transition from __dirname to import.meta.url could be better documented #28114
We could improve the documentation: import.meta.url is not an equivalent of __dirname several lines of code are required to make it work.
Here's my current workaround, if you have a better way of doing please share:
import path from "path";
// same as previous __dirname, substring(8) removes the file:/// that is at the beginning
const dirname__ = path.dirname(import.meta.url.substring(8));
const x = readFileSync(`${dirname__}/x.html`, `utf8`);
Describe the solution you'd like
Add a few lines in the doc on how to transition from __dirname to import.meta.url
Describe alternatives you've considered
Otherwise provide import.meta.__dirname for even easier transition
@GrosSacASac You are exactly right this is an important transition to document, so thank you for bringing this up. Note that your example is not correct - you should use the following pattern rather in Node.js:
@GrosSacASac You are exactly right this is an important transition to document, so thank you for bringing this up. Note that your example is not correct - you should use the following pattern rather in Node.js:
If you'd be interested in submitting a docs PR to clarify the section you linked, please do, that would definitely be useful.
Just a slight update to @guybedford's excellent comment. That code won't produce the equivalent of __dirname, but this one would with a slight adjustment:
Activity
guybedford commentedon Jun 7, 2019
@GrosSacASac You are exactly right this is an important transition to document, so thank you for bringing this up. Note that your example is not correct - you should use the following pattern rather in Node.js:
See https://nodejs.org/dist/latest-v12.x/docs/api/url.html#url_url_fileurltopath_url for more info on this method.
If you'd be interested in submitting a docs PR to clarify the section you linked, please do, that would definitely be useful.
GrosSacASac commentedon Jun 13, 2019
Thanks I'll do some something
doc: esm add example how to create __filename, __dirname
doc: add example on how to create __filename, __dirname for esm
doc: add example on how to create __filename, __dirname for esm
Please ignore this msg (#1)
Fixing template path not found issue. Problem references id in import…
scalvert commentedon Aug 6, 2021
Just a slight update to @guybedford's excellent comment. That code won't produce the equivalent of
__dirname, but this one would with a slight adjustment:The former example will return the file path, not the directory.
GrosSacASac commentedon Aug 6, 2021
it was changed https://github.com/nodejs/node/pull/28282/files