interface
ImportMeta
interface ImportMeta
The type of import.meta.
If you need to declare that a given property exists on import.meta, this type may be augmented via interface merging.
- readonly dir: string
Absolute path to the directory containing the source file, without a trailing slash
- readonly env: Env & ProcessEnv & ImportMetaEnv
The environment variables of the process
import.meta.env === process.env - hot: { data: any; accept(): void; dispose(cb: (data: any) => void | Promise<void>): void; off(event: HMREvent, callback: () => void): void; on(event: HMREvent, callback: () => void): void }
Hot module replacement (HMR) APIs. In production this value is
undefined, so you can guard HMR code with anifstatement:if (import.meta.hot) { // HMR APIs are available }The check is usually unnecessary: Bun dead-code-eliminates calls to the HMR APIs in production builds.
https://bun.com/docs/bundler/hmr
- main: boolean
trueif the current file started the processif (import.meta.main) { console.log("I started the process!"); } - require: Require
Load a CommonJS module within an ES Module. Bun's transpiler rewrites all calls to
requirewithimport.meta.requirewhen transpiling ES Modules for the runtime.Warning: This API is not stable and may change or be removed in the future. Use at your own risk.
- url: string
file://url string for the current module.console.log(import.meta.url); "file:///Users/me/projects/my-app/src/my-app.ts" - specifier: string,): string;
import.meta.resolveis a module-relative resolution function scoped to each module, returning the URL string.const dependencyAsset = import.meta.resolve('component-lib/asset.css'); // file:///app/node_modules/component-lib/asset.css import.meta.resolve('./dep.js'); // file:///app/dep.jsAll features of the Node.js module resolution are supported. Dependency resolutions are subject to the permitted exports resolutions within the package.
Caveats:
- This can result in synchronous file-system operations, which can impact performance similarly to
require.resolve. - This feature is not available within custom loaders (it would create a deadlock).
@param specifierThe module specifier to resolve relative to the current module.
@param parentAn optional absolute parent module URL to resolve from. Default:
import.meta.url@returnsThe absolute URL string that the specifier would resolve to.
- This can result in synchronous file-system operations, which can impact performance similarly to