Skip to content

feat: add support for import.meta.dirname and import.meta.filename#782

Open
hnshah wants to merge 1 commit intoprivatenumber:masterfrom
hnshah:feat/import-meta-dirname-filename
Open

feat: add support for import.meta.dirname and import.meta.filename#782
hnshah wants to merge 1 commit intoprivatenumber:masterfrom
hnshah:feat/import-meta-dirname-filename

Conversation

@hnshah
Copy link

@hnshah hnshah commented Mar 24, 2026

Fixes #781

Adds support for Node.js 20.11+ import.meta.dirname and import.meta.filename properties.

Problem

  • .js files transformed to CJS had undefined for these properties
  • Caused TypeError crashes when code tried to use them
  • .ts and .mjs files already worked (different code path)

Solution

Follow same pattern as import.meta.url using esbuild define.

Testing

# Before: tsx test.js → undefined, undefined
# After: tsx test.js → /path/to/dir, /path/to/dir/test.js

Reference: unjs/jiti#308

Fixes privatenumber#781

Adds support for Node.js 20.11+ import.meta.dirname and import.meta.filename
properties when transforming .js files to CommonJS format.

These properties were already working for .ts and .mjs files (which use ESM
format), but were undefined for .js files that tsx converts to CJS.

Implementation follows the same pattern as existing import.meta.url support,
using esbuild's define option to inject the values during transformation.

Reference: unjs/jiti#308
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support import.meta.dirname and import.meta.filename

1 participant