GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — modules: add import.meta.__dirname and import.meta.__filename by jasnell · Pull Request #39147 · nodejs/node

Conversation

@jasnell

@jasnell jasnell commented Jun 24, 2021

Copy link
Copy Markdown
Member

To help ease the transition from existing CommonJS code to
ESM, adds convenience properties to import.meta that are
explicitly marked "Legacy" in the documentation.

This may or may not be a good idea, but I've run into this
enough times that I figured I'd at least see. If folks don't
think this is a good idea, then I'm fine with it not landing.

const {
  __filename,
  __dirname,
} = import.meta;

They are only available when file:// modules are used.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

himself65 reacted with eyes emoji
All reactions
  • 👀 1 reaction
@jasnell
modules: add import.meta.__dirname and import.meta.__filename
41bbc55
To help ease the transition from existing CommonJS code to
ESM, adds convenience properties to `import.meta` that are
explicitly marked "Legacy" in the documentation.

Signed-off-by: James M Snell <[email protected]>
@github-actions github-actions Bot added esm Issues and PRs related to the ECMAScript Modules implementation. needs-ci PRs that need a full CI run. labels Jun 24, 2021
@jasnell

jasnell commented Jun 24, 2021

Copy link
Copy Markdown
Member Author

If this did happen to land, I'd suggest it would be a good idea to contribute an eslint rule that flags uses and recommends the equivalent import.meta.url uses instead.

aduh95 reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

VoltrexKeyva
VoltrexKeyva reviewed Jun 24, 2021
View reviewed changes
Comment thread lib/internal/modules/esm/translators.js
ArrayPrototypeMap,
Boolean,
JSONParse,
ObjectDefineProperties,

@VoltrexKeyva VoltrexKeyva Jun 24, 2021

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
ObjectDefineProperties,

Unused primordial.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@devsnek

devsnek commented Jun 24, 2021

Copy link
Copy Markdown
Member

If these are legacy, that implies a deprecation/transition period right? Would migrating a file from cjs to esm not already be that transition? People will just use these and then we'll have to support them forever even though they're bad.

DRoet, KhafraDev, bricss, and ZBemo reacted with thumbs up emoji
All reactions
  • 👍 4 reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@jasnell

jasnell commented Jun 24, 2021

Copy link
Copy Markdown
Member Author

To be clear, legacy status does not imply eventual deprecation.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

bmeck
bmeck reviewed Jun 25, 2021
View reviewed changes
Comment thread doc/api/esm.md
`__dirname` pseudo-global. Specifically, it returns the result of converting
the `import.meta.url` into a file path then returning the directory portion.

The property is only available when `import.meta.url` specifies a `file://`

@bmeck bmeck Jun 25, 2021

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the check in the impl only uses file: should you be using // in the impl or remove that bit from here to just be file:? generally when talking about scheme we just have scheme: (E.G. https://nodejs.org/dist/latest-v14.x/docs/api/esm.html#esm_file_urls )

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
bmeck
bmeck reviewed Jun 25, 2021
View reviewed changes
Comment thread lib/internal/modules/esm/translators.js
Comment on lines +135 to +136
meta.__filename = internalURLModule.fileURLToPath(new URL(url));
meta.__dirname = internalPathModule.dirname(meta.__filename);

@bmeck bmeck Jun 25, 2021

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can this be done with getters / lazily?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@jasnell jasnell Jun 25, 2021

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately not, there's a test that checks that import.meta properties are writable

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@bmeck bmeck Jun 25, 2021

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems fine

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@targos

targos commented Jun 25, 2021 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

As an alternative or in parallel, I think we should also improve the documentation at https://nodejs.org/dist/latest-v16.x/docs/api/esm.html#esm_no_filename_or_dirname or https://nodejs.org/dist/latest-v16.x/docs/api/esm.html#esm_import_meta_url, by giving examples of how common use cases can be done with import.meta.url

devsnek, KhafraDev, bricss, himself65, and theoludwig reacted with thumbs up emoji bricss reacted with heart emoji
All reactions
  • 👍 5 reactions
  • ❤️ 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bricss

bricss commented Jun 28, 2021

Copy link
Copy Markdown
Contributor

I was hoping that one day we will get rid of CJS legacy aka __filename, __dirname, but yet it's coming back 🥶

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bmeck

bmeck commented Jun 28, 2021

Copy link
Copy Markdown
Member

Do we have a clear problem with including these? It won't cause a static unrecoverable error if these values are used.

My main concern is this just adds 1 more code migration, though it is incremental. Adding them to import.meta still requires that code be updated so that they can be used. It also still means that examples are not shared between CJS and ESM:

// __dirname is a ReferenceError here
fs.readFile(import.meta.__dirname);
// import.meta is a SyntaxError here
fs.readFile(__dirname);

I think an approach that allows both CJS and ESM to share the code would be somewhat preferable to this, but I don't have any major concerns w/ these being added except that they would be wasted compute for any module not using them and using import.meta.url (and vice versa).

aduh95, bricss, and theoludwig reacted with thumbs up emoji
All reactions
  • 👍 3 reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@jasnell

jasnell commented Jun 29, 2021

Copy link
Copy Markdown
Member Author

given the controversy here and given that I'm not 100% bought in to this idea either, I'm going to go ahead and close this :-)

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@jasnell jasnell closed this Jun 29, 2021
@mscdex mscdex mentioned this pull request Jul 11, 2023
esm: add import.meta.dirname and import.meta.filename #48740
Merged
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

2 more reviewers
@bmeck bmeck bmeck left review comments
@VoltrexKeyva VoltrexKeyva VoltrexKeyva left review comments
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

esm Issues and PRs related to the ECMAScript Modules implementation. needs-ci PRs that need a full CI run.

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

6 participants

@jasnell @devsnek @targos @bricss @bmeck @VoltrexKeyva
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.