mirror of
https://github.com/oven-sh/bun
synced 2026-02-13 12:29:07 +00:00
2.6 KiB
2.6 KiB
The import.meta object is a way for a module to access information about itself. It's part of the JavaScript language, but its contents are not standardized. Each "host" (browser, runtime, etc) is free to implement any properties it wishes on the import.meta object.
Bun implements the following properties.
import.meta.dir; // => "/path/to/project"
import.meta.file; // => "file.ts"
import.meta.path; // => "/path/to/project/file.ts"
import.meta.url; // => "file:///path/to/project/file.ts"
import.meta.main; // `true` if this file is directly executed by `bun run`
// `false` otherwise
import.meta.resolve("zod"); // => "file:///path/to/project/node_modules/zod/index.js"
import.meta.glob("./dir/*.ts"); // => { "./dir/a.ts": () => import("./dir/a.ts"), ... }
{% table %}
import.meta.dir- Absolute path to the directory containing the current file, e.g.
/path/to/project. Equivalent to__dirnamein CommonJS modules (and Node.js)
import.meta.dirname- An alias to
import.meta.dir, for Node.js compatibility
import.meta.env- An alias to
process.env.
import.meta.file- The name of the current file, e.g.
index.tsx
import.meta.path- Absolute path to the current file, e.g.
/path/to/project/index.ts. Equivalent to__filenamein CommonJS modules (and Node.js)
import.meta.filename- An alias to
import.meta.path, for Node.js compatibility
import.meta.main- Indicates whether the current file is the entrypoint to the current
bunprocess. Is the file being directly executed bybun runor is it being imported?
-
import.meta.resolve -
Resolve a module specifier (e.g.
"zod"or"./file.tsx") to a url. Equivalent toimport.meta.resolvein browsersimport.meta.resolve("zod"); // => "file:///path/to/project/node_modules/zod/index.ts"
import.meta.url- A
stringurl to the current file, e.g.file:///path/to/project/index.ts. Equivalent toimport.meta.urlin browsers
-
import.meta.glob -
Import multiple modules using glob patterns. Returns an object mapping file paths to lazy-loading functions.
const modules = import.meta.glob("./modules/*.js"); // const modules = { // './modules/a.js': () => import('./modules/a.js'), // './modules/b.js': () => import('./modules/b.js'), // }
{% /table %}