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 — esm: add import.meta.dirname and import.meta.filename by jsumners · Pull Request #48740 · nodejs/node

Conversation

@jsumners

@jsumners jsumners commented Jul 11, 2023 •
edited by GeoffreyBooth
Loading

Uh oh!

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

Copy link
Copy Markdown
Contributor

This PR adds import.meta.dirname and import.meta.filename for ECMAScript Modules that are loaded from the local filesystem. In my view, these properties don’t make any sense for modules loaded from the network, but they are very desired for local modules. At least one recent issue (#47756) wanted them, and there exists at least two ecosystem modules to solve for them:

  • https://www.npmjs.com/package/umeta
  • https://www.npmjs.com/package/desm

The Problem

As it stands, to determine the containing directory of a script we must write code like:

import url from 'url'
import path from 'path'
const dirname = path.dirname(url.fileURLToPath(import.meta.url))

Of course, this needs to be repeated in every script that needs the information. With this PR, the code is reduced to:

const dirname = import.meta.__dirname

The developer doesn’t need to remember how to convert the file URL to a path and then resolve the directory from that.

External Discussion

The impetus for this PR is a recent Twitter discussion – https://twitter.com/jasnell/status/1677335322978295809

Notable change

In file:-based ES modules, new properties import.meta.filename and import.meta.dirname provide equivalents to CommonJS __filename and __dirname. In particular, import.meta.filename provides the full absolute path (as a file path, not URL) to the module; and import.meta.dirname provides the full absolute path to the module’s containing folder. These properties are missing for non-file:-based ES modules, such as those loaded from data: or https: URLs.

Sorry, something went wrong.

Uh oh!

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

MoLow, GlenTiki, gengjiawen, atlowChemi, bricss, tlhunter, bakkot, Lehoczky, Mifrill, simonkberg, and 38 more reacted with thumbs up emoji jimmywarting reacted with thumbs down emoji tniessen reacted with confused emoji jcbhmr, bricss, lin72h, ghiscoding, ximex, miclgael, Razem, svieira, MaartenStaa, tim545, and 4 more reacted with rocket emoji
All reactions
  • 👍 48 reactions
  • 👎 1 reaction
  • 😕 1 reaction
  • 🚀 14 reactions
@nodejs-github-bot nodejs-github-bot added esm Issues and PRs related to the ECMAScript Modules implementation. needs-ci PRs that need a full CI run. labels Jul 11, 2023
mcollina
mcollina approved these changes Jul 11, 2023
View reviewed changes

@mcollina mcollina left a comment

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.

lgtm

Sorry, something went wrong.

Uh oh!

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

All reactions
@mcollina mcollina added notable-change PRs with changes that should be highlighted in changelogs. semver-minor PRs that contain new features and should be released in the next minor version. labels Jul 11, 2023
@github-actions

github-actions Bot commented Jul 11, 2023

Copy link
Copy Markdown
Contributor

The notable-change PRs with changes that should be highlighted in changelogs. label has been added by @mcollina.

Please suggest a text for the release notes if you'd like to include a more detailed summary, then proceed to update the PR description with the text or a link to the notable change suggested text comment.

All reactions

Sorry, something went wrong.

Uh oh!

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

@mcollina mcollina added the request-ci Add this label to start a Jenkins CI on a PR. Only starts once the PR has an approving review. label Jul 11, 2023
bmeck
bmeck reviewed Jul 11, 2023
View reviewed changes
Comment thread lib/internal/modules/esm/initialize_import_meta.js Resolved

Uh oh!

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

bmeck
bmeck reviewed Jul 11, 2023
View reviewed changes
Comment thread test/es-module/test-esm-import-meta.mjs Resolved

Uh oh!

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

bmeck
bmeck reviewed Jul 11, 2023
View reviewed changes
Comment thread doc/api/esm.md Resolved

Uh oh!

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

bmeck
bmeck reviewed Jul 11, 2023
View reviewed changes
Comment thread doc/api/esm.md Resolved

Uh oh!

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

@github-actions github-actions Bot removed the request-ci Add this label to start a Jenkins CI on a PR. Only starts once the PR has an approving review. label Jul 11, 2023
bmeck
bmeck reviewed Jul 11, 2023
View reviewed changes
Comment thread lib/internal/modules/esm/initialize_import_meta.js Resolved

Uh oh!

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

@mscdex

mscdex commented Jul 11, 2023

Copy link
Copy Markdown
Contributor

This was suggested before in #39147. Has anything changed since then?

All reactions

Sorry, something went wrong.

Uh oh!

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

VoltrexKeyva
VoltrexKeyva reviewed Jul 12, 2023
View reviewed changes
Comment thread doc/api/esm.md Resolved

Uh oh!

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

Comment thread doc/api/esm.md Resolved

Uh oh!

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

@mcollina

mcollina commented Jul 12, 2023

Copy link
Copy Markdown
Member

This was suggested before in #39147. Has anything changed since then?

I think it is less controversial now. Everybody migrating to ESM needs to figure this out and it requires way too many steps to get those values.

jsumners, gengjiawen, Linkgoron, Qard, tlhunter, rschristian, ghiscoding, 0xdevalias, and borys-p reacted with thumbs up emoji jcbhmr reacted with confused emoji
All reactions
  • 👍 9 reactions
  • 😕 1 reaction

Sorry, something went wrong.

Uh oh!

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

@jsumners
jsumners force-pushed the esm-dir-file branch 2 times, most recently from dac12b1 to cf0841f Compare July 12, 2023 12:36
@jcbhmr

jcbhmr commented Jul 12, 2023 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Contributor

This may be of no concern, but Bun appears to use import.meta.dir and import.meta.path for these values: https://bun.sh/docs/api/import-meta idk if that's relevant tho. 🤷‍♂️

All reactions

Sorry, something went wrong.

Uh oh!

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

aduh95
aduh95 reviewed Jul 12, 2023
View reviewed changes

@aduh95 aduh95 left a comment

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.

I don't think it's a good idea to introduce this, we should move away from paths IMO.

Sorry, something went wrong.

Uh oh!

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

jcbhmr, KhafraDev, gurgunday, bricss, jimmywarting, and nicolo-ribaudo reacted with thumbs up emoji jcbhmr reacted with hooray emoji
All reactions
  • 👍 6 reactions
  • 🎉 1 reaction
Comment thread lib/internal/modules/esm/initialize_import_meta.js Resolved

Uh oh!

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

Comment thread test/es-module/test-esm-import-meta.mjs Resolved

Uh oh!

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

Comment thread test/es-module/test-esm-import-meta.mjs Resolved

Uh oh!

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

Comment thread test/es-module/test-esm-import-meta.mjs Resolved

Uh oh!

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

Comment thread lib/internal/modules/esm/initialize_import_meta.js Resolved

Uh oh!

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

Comment thread lib/internal/modules/esm/initialize_import_meta.js Resolved

Uh oh!

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

@aduh95

aduh95 commented Jul 12, 2023

Copy link
Copy Markdown
Contributor

Everybody migrating to ESM needs to figure this out and it requires way too many steps to get those values.

Why do you need paths though? It shouldn't be necessary, URL should get you covered.

jcbhmr, anonrig, bricss, and jimmywarting reacted with thumbs up emoji jcbhmr reacted with rocket emoji
All reactions
  • 👍 4 reactions
  • 🚀 1 reaction

Sorry, something went wrong.

Uh oh!

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

@mcollina

mcollina commented Jul 13, 2023

Copy link
Copy Markdown
Member

Why do you need paths though? It shouldn't be necessary, URL should get you covered.

Most APIs from fs and path do not work well with URLs. Reading files relative to the current one is something everybody does all the time for all sorts of purposes. Currently, it takes quite a few lines of code to get the path for a file in the same directory:

import { fileURLToPath } from 'url'
import { dirname, join } from 'path'

const __dirname = dirname(url.fileURLToPath(import.meta.url))
const toAccess = join(__dirname, 'another_file')
All reactions

Sorry, something went wrong.

Uh oh!

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

@aduh95

aduh95 commented Jul 13, 2023

Copy link
Copy Markdown
Contributor

Most APIs from fs

That's wrong I think, all node:fs APIs works with either. If you see issues with node:fs and URL, please open an issue, that'd be a bug.

and path

That's kinda obvious, node:path is for paths, not URLs.

Reading files relative to the current one is something everybody does all the time for all sorts of purposes.

That's my point, you should be using URL for that. The equivalent to your code snippet would be:

const toAccess = new URL('./another_file', import.meta.url);
bricss, jcbhmr, jimmywarting, nulladdict, trusktr, felixfbecker, and electrovir reacted with thumbs up emoji
All reactions
  • 👍 7 reactions

Sorry, something went wrong.

Uh oh!

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

aduh95
aduh95 previously requested changes Jul 13, 2023
View reviewed changes

@aduh95 aduh95 left a comment

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.

Adding an explicit -1 on this because it already has an approval and I don't want it to land without some extensive discussion first.

I think this goes in the wrong direction. I'm happy to be proven wrong, but I guess there's also an ideological problem: I think using paths in ESM is a faux-pas, if one goes ESM, they should let paths be a relic of the past, and embrace URLs. I can see it can certainly be useful to have the proposed properties to convert a CJS module to ESM, but someone writing ESM from scratch should never need it, and it would be confusing to add something that works only on file: protocol, when I would prefer to see Node.js go in the direction of being more protocol agnostic.

Sorry, something went wrong.

Uh oh!

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

jcbhmr, gurgunday, bricss, lucacasonato, nulladdict, trusktr, nicolo-ribaudo, jimmywarting, and wooorm reacted with thumbs up emoji Qard reacted with thumbs down emoji jcbhmr reacted with rocket emoji
All reactions
  • 👍 9 reactions
  • 👎 1 reaction
  • 🚀 1 reaction
@jsumners
jsumners force-pushed the esm-dir-file branch from bcffca1 to b529e80 Compare July 13, 2023 11:54
@MoLow

MoLow commented Jul 13, 2023

Copy link
Copy Markdown
Member

I think this goes in the wrong direction. I'm happy to be proven wrong, but I guess there's also an ideological problem: I think using paths in ESM is a faux-pas, if one goes ESM, they should let paths be a relic of the past, and embrace URLs. I can see it can certainly be useful to have the proposed properties to convert a CJS module to ESM, but someone writing ESM from scratch should never need it, and it would be confusing to add something that works only on file: protocol, when I would prefer to see Node.js go in the direction of being more protocol agnostic.

We should also consider if it will help the adoption of ESM and make migration easier / less painful.
even if there is an ideological issue - it is not very pragmatic to only provide a solution for "someone writing ESM from scratch".
even if one can argue it will never be needed, avoiding such utilities/helpers because they seem redundant can have a negative effect on the ecosystem.

jsumners, ruyadorno, and tim545 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.

gengjiawen
gengjiawen approved these changes Jul 13, 2023
View reviewed changes

@gengjiawen gengjiawen left a comment

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.

Almost shed tears, thank you

Sorry, something went wrong.

Uh oh!

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

jsumners, ghiscoding, junaga, guansss, and 0xdevalias reacted with heart emoji
All reactions
  • ❤️ 5 reactions
@gengjiawen

gengjiawen commented Jul 13, 2023

Copy link
Copy Markdown
Member

if one goes ESM, they should let paths be a relic of the past, and embrace URLs.

It's like a muscle memory even can trace from python. It can avoid many mental burden from using esm, this is my mosted missed feature in esm.

Also many people trapped on this. lots of libs goto full esm have this info everywhere.

It's a good move in the long run, and way friendly for users back then when only cjs exists.

MoLow, mcollina, rschristian, ghiscoding, and Nemikolh reacted with thumbs up emoji
All reactions
  • 👍 5 reactions

Sorry, something went wrong.

Uh oh!

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

@gengjiawen

gengjiawen commented Jul 13, 2023

Copy link
Copy Markdown
Member

I will share some my work experience, we used js almost everywhere, some are cjs, some are esm. When it comes to script job in CI and linux server, it's always have been cjs module. because __dirname and __filename used everywhere just like python. Esm equivalence is way verbose and less instinctive.

jimmywarting reacted with thumbs down emoji mcollina and Nemikolh reacted with heart emoji
All reactions
  • 👎 1 reaction
  • ❤️ 2 reactions

Sorry, something went wrong.

Uh oh!

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

@mcollina

mcollina commented Jul 13, 2023

Copy link
Copy Markdown
Member

@aduh95 The vast majority of the modules on npm only accept paths, not URLs. Therefore it adds quite a bit of a mental burden to do that. The recommendation you have is for all modules on NPM to start accepting file:// URLs as well as paths, or even only URLs. I don't think that's feasible.

Even modules that went all-in on ESM only use paths everywhere and not URLs, as an example take a look at https://vitest.dev/guide/#workspaces-support.

Overall I think adding __filename and __dirname will help the adoption of ESM.

jsumners, benjamingr, gurgunday, shalvah, aladdin-add, and ghiscoding reacted with thumbs up emoji jimmywarting reacted with thumbs down emoji
All reactions
  • 👍 6 reactions
  • 👎 1 reaction

Sorry, something went wrong.

Uh oh!

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

This was referenced Apr 10, 2026
feat: initial implementation leap0-dev/langchain-leap0-js#1
Merged
feat(asset): WP-020 스케치 에셋 매니페스트 시스템 구현 (#47) cgm-16/weatherpane#53
Merged
TypeScript ESM rewrite with Result API and CI refresh oleg-koval/trembita#323
Merged
chore: migrate from tsup to tsdown zenstackhq/zenstack#2580
Merged
Enhance HTTP security and YUCP trust bundle handling Yeusepe/YUCP-Creator-Assistant#18
Merged
Resolve CSS variable references in token extraction brendanciccone/storysync#7
Merged
This was referenced Apr 18, 2026
docs: add OpenSSF Best Practices badge to README klodr/faxdrop-mcp#8
Merged
test: expand backend test coverage for stats and history edge cases tomtranjr/docqflow#14
Merged
Drop Node 18, modernize toolchain (zod 4, eslint 10, ts 6, vitest) — v0.8.0 klodr/mercury-invoicing-mcp#35
Merged
feat(gateway-admin): chat UI + Aurora token sweep — v0.7.0/0.7.1 dinglebear-ai/labby#27
Merged
Tighten runtime packaging and version cache behavior imjlk/wp-typia#554
Merged
This was referenced Apr 27, 2026
fix Windows node-pty packaging freshman515/FastAgent#1
Closed
Track B Day 2: zg-bridge + Python ComputeClient cirsteve/eerful#2
Merged
Polish repo tooling, docs, and quality automation ogiboy/agentic-trader#10
Merged
fix(frontend): restore Tailwind asset delivery guards Katsiarynakavaleuskaya/PulsePlate#1576
Merged
correct payment stripe mAmineChniti/accountia-web#148
Merged
feat(monorepo): scaffold events plugin workspace [PRO-401] foreztgump/dateline-events-plugin#14
Merged
Add Project models and optimize Prisma client configuration AshwinM-134/ghost-ai#2
Merged
This was referenced May 4, 2026
Development bluebaney10/ghost-ai#4
Open
feat(form-core): 5-10x faster makePathArray TanStack/form#2152
Merged
feat: add TypeScript directive observability tooling pertrai1/agent-directives#16
Merged
This was referenced May 13, 2026
initialize pnpm workspace with strict TS, ESLint, Prettier SaiCharan99/VideoGenAI#1
Merged
feat(ui): aligne la modale sur le design system DSTET incubateur-ademe/territoires-en-transitions#4461
Merged
This was referenced May 20, 2026
feat(GXPM-156): Phase → Required Skill 合同衔接机制 laozhong86/gxpm#52
Merged
feat(dashboards): DashboardApp factory + all 8 dashboard migrations genesiscz/GenesisTools#182
Merged
This was referenced May 28, 2026
Support Android and fix slow torrent YueMiyuki/Risuko#85
Merged
Migrate from Electron to Tauri v2 Ntureyc/BitBeat#1
Merged
@coderabbitai coderabbitai Bot mentioned this pull request Jun 10, 2026
refactor(cli): extract CLI-plugin contract into @forinda/kickjs-cli-kit forinda/kick-js#333
Merged
@coderabbitai coderabbitai Bot mentioned this pull request Jul 29, 2026
feat: add atomic cmux goto navigation manaflow-ai/cmux#9109
Open
@coderabbitai coderabbitai Bot mentioned this pull request Aug 7, 2026
Refactor/database package exports RaplesWojtyla/KarmAgent#10
Merged
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@tniessen tniessen tniessen left review comments
@aduh95 aduh95 aduh95 left review comments
@mcollina mcollina mcollina approved these changes
@Qard Qard Qard approved these changes
@jasnell jasnell jasnell approved these changes
@GeoffreyBooth GeoffreyBooth GeoffreyBooth approved these changes
@benjamingr benjamingr benjamingr approved these changes
@gengjiawen gengjiawen gengjiawen approved these changes
@JakobJingleheimer JakobJingleheimer JakobJingleheimer left review comments
+3 more reviewers
@bmeck bmeck bmeck left review comments
@Mifrill Mifrill Mifrill 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

commit-queue-squash PRs the Commit Queue should land as one squashed commit. esm Issues and PRs related to the ECMAScript Modules implementation. needs-ci PRs that need a full CI run. notable-change PRs with changes that should be highlighted in changelogs. semver-minor PRs that contain new features and should be released in the next minor version.

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.

20 participants

@jsumners @mscdex @mcollina @jcbhmr @aduh95 @MoLow @gengjiawen @richardlau @bricss @cjihrig @nodejs-github-bot @bmeck @LiviaMedeiros @targos @GeoffreyBooth @izaakschroeder @WebReflection @Qard @guybedford @jasnell
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.