Skip to content

Node compat: Add import.meta.dirname and import.meta.filename #7778

@risu729

Description

@risu729

What version of Bun is running?

1.0.18+36c316a24

What platform is your computer?

Linux 5.15.0-1048-oracle aarch64 aarch64

What steps can reproduce the bug?

// test.mjs
console.log("dir:", import.meta.dir);
console.log("dirname:", import.meta.dirname);
console.log("file:", import.meta.file);
console.log("filename:", import.meta.filename);

What is the expected behavior?

$ node test.mjs
dir: undefined
dirname: /home/ubuntu
file: undefined
filename: /home/ubuntu/test.mjs

What do you see instead?

$ bun run test.mjs
dir: /home/ubuntu
dirname: undefined
file: test.mjs
filename: undefined

Additional information

Since Node.js v21.2.0, import.meta.dirname and import.meta.filename are added. nodejs/node#48740

import.meta.dirname can be added to Bun as an alias for import.meta.dir.

However, while import.meta.filename in Node.js includes the directory but import.meta.file in Bun does not, import.meta.filename would not be just an alias.
import.meta.filename equals ${import.meta.dir}/${import.meta.file}.

I think the behavior might cause some confusion, but I believe that adding these properties would improve compatibility with Node.js.

Node.js docs: https://nodejs.org/api/esm.html#importmeta
Bun docs: https://bun.sh/docs/api/import-meta

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingnode.jsCompatibility with Node.js APIs

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions