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

  • dirname: string

    Alias of import.meta.dir. Exists for Node.js compatibility

  • readonly env: Env & ProcessEnv & ImportMetaEnv

    The environment variables of the process

    import.meta.env === process.env

    Under bun test, it's a view of process.env that also has Vite's MODE, DEV, PROD, SSR and BASE_URL.

  • readonly file: string

    Filename of the source file

  • filename: string

    Alias of import.meta.path. Exists for Node.js compatibility

  • 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 an if statement:

    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

    true if the current file started the process

    if (import.meta.main) {
     console.log("I started the process!");
    }
  • readonly path: string

    Absolute path to the source file

  • require: Require

    Load a CommonJS module within an ES Module. Bun's transpiler rewrites all calls to require with import.meta.require when 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"
  • glob<TModule = unknown>(
    pattern: string | string[],
    options?: ImportMetaGlobOptions & { eager: false }
    ): Record<string, () => Promise<TModule>>;

    Import every module that matches a glob pattern. Compatible with Vite's import.meta.glob.

    Bun's transpiler and bundler replace the call with an object literal, so the arguments must be literals.

    const pages = import.meta.glob("./pages/*.tsx");
    // {
    //   "./pages/about.tsx": () => import("./pages/about.tsx"),
    //   "./pages/home.tsx": () => import("./pages/home.tsx"),
    // }
    glob<TModule = unknown>(
    pattern: string | string[],
    options: ImportMetaGlobOptions & { eager: true }
    ): Record<string, TModule>;
    const pages = import.meta.glob("./pages/*.tsx", { eager: true });
    // import * as about from "./pages/about.tsx";
    // import * as home from "./pages/home.tsx";
    // { "./pages/about.tsx": about, "./pages/home.tsx": home }
  • specifier: string
    ): string;
    specifier: string,
    parent?: string | URL
    ): string;