frontend
    Preparing search index...

    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.

    interface ImportMeta {
        dir: string;
        dirname: string;
        env: Env & ProcessEnv & ImportMetaEnv;
        file: string;
        filename: string;
        hot: {
            data: any;
            accept(): void;
            accept(cb: (newModule: any) => void): void;
            accept(specifier: string, callback: (newModule: any) => void): void;
            accept(specifiers: string[], callback: (newModules: any[]) => void): void;
            decline(): void;
            dispose(cb: (data: any) => void | Promise<void>): void;
            off(event: HMREvent, callback: () => void): void;
            on(event: HMREvent, callback: () => void): void;
        };
        main: boolean;
        path: string;
        require: Require;
        url: string;
        resolve(specifier: string): string;
        resolve(specifier: string, parent?: string | URL): string;
        resolveSync(moduleId: string, parent?: string): string;
    }
    Index

    Properties

    dir: string

    Absolute path to the directory containing the source file.

    Does not have a trailing slash

    dirname: string

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

    env: Env & ProcessEnv & ImportMetaEnv

    The environment variables of the process

    import.meta.env === process.env
    
    file: string

    Filename of the source file

    filename: string

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

    hot: {
        data: any;
        accept(): void;
        accept(cb: (newModule: any) => void): void;
        accept(specifier: string, callback: (newModule: any) => void): void;
        accept(specifiers: string[], callback: (newModules: any[]) => void): void;
        decline(): void;
        dispose(cb: (data: any) => void | Promise<void>): void;
        off(event: HMREvent, callback: () => void): void;
        on(event: HMREvent, callback: () => void): void;
    }

    Hot module replacement APIs. This value is undefined in production and can be used in an if statement to check if HMR APIs are available

    if (import.meta.hot) {
    // HMR APIs are available
    }

    However, this check is usually not needed as Bun will dead-code-eliminate calls to all of the HMR APIs in production builds.

    https://bun.com/docs/bundler/hmr

    Type Declaration

    • data: any

      import.meta.hot.data maintains state between module instances during hot replacement, enabling data transfer from previous to new versions. When import.meta.hot.data is written to, Bun will mark this module as capable of self-accepting (equivalent of calling accept()).

      const root = import.meta.hot.data.root ??= createRoot(elem);
      root.render(<App />); // re-use an existing root

      In production, data is inlined to be {}. This is handy because Bun knows it can minify {}.prop ??= value into value in production.

    • accept: function
      • Indicate that this module can be replaced simply by re-evaluating the file. After a hot update, importers of this module will be automatically patched.

        When import.meta.hot.accept is not used, the page will reload when the file updates, and a console message shows which files were checked.

        Returns void

        import { getCount } from "./foo";

        console.log("count is ", getCount());

        import.meta.hot.accept();
      • Indicate that this module can be replaced by evaluating the new module, and then calling the callback with the new module. In this mode, the importers do not get patched. This is to match Vite, which is unable to patch their import statements. Prefer using import.meta.hot.accept() without an argument as it usually makes your code easier to understand.

        When import.meta.hot.accept is not used, the page will reload when the file updates, and a console message shows which files were checked.

        Parameters

        • cb: (newModule: any) => void

        Returns void

        export const count = 0;

        import.meta.hot.accept((newModule) => {
        if (newModule) {
        // newModule is undefined when SyntaxError happened
        console.log('updated: count is now ', newModule.count)
        }
        });

        In production, calls to this are dead-code-eliminated.

      • Indicate that a dependency's module can be accepted. When the dependency is updated, the callback will be called with the new module.

        When import.meta.hot.accept is not used, the page will reload when the file updates, and a console message shows which files were checked.

        Parameters

        • specifier: string
        • callback: (newModule: any) => void

        Returns void

        import.meta.hot.accept('./foo', (newModule) => {
        if (newModule) {
        // newModule is undefined when SyntaxError happened
        console.log('updated: count is now ', newModule.count)
        }
        });
      • Indicate that a dependency's module can be accepted. This variant accepts an array of dependencies, where the callback will receive the one updated module, and undefined for the rest.

        When import.meta.hot.accept is not used, the page will reload when the file updates, and a console message shows which files were checked.

        Parameters

        • specifiers: string[]
        • callback: (newModules: any[]) => void

        Returns void

    • decline: function
      • No-op

        Returns void

    • dispose: function
      • Attach an on-dispose callback. This is called:

        • Just before the module is replaced with another copy (before the next is loaded)
        • After the module is detached (removing all imports to this module)

        This callback is not called on route navigation or when the browser tab closes.

        Returning a promise will delay module replacement until the module is disposed. All dispose callbacks are called in parallel.

        Parameters

        • cb: (data: any) => void | Promise<void>

        Returns void

    • off: function
      • Stop listening for an event from the dev server

        For compatibility with Vite, event names are also available via vite:* prefix instead of bun:*.

        https://bun.com/docs/bundler/hmr#import-meta-hot-on-and-off

        Parameters

        • event: HMREvent

          The event to stop listening to

        • callback: () => void

          The callback to stop listening to

        Returns void

    • on: function
      • Listen for an event from the dev server

        For compatibility with Vite, event names are also available via vite:* prefix instead of bun:*.

        https://bun.com/docs/bundler/hmr#import-meta-hot-on-and-off

        Parameters

        • event: HMREvent

          The event to listen to

        • callback: () => void

          The callback to call when the event is emitted

        Returns void

    main: boolean

    Did the current file start the process?

    if (import.meta.main) {
    console.log("I started the process!");
    }
    console.log(
    import.meta.main === (import.meta.path === Bun.main)
    )
    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"

    Methods

    • Parameters

      • specifier: string

      Returns string

    • Parameters

      • specifier: string
      • Optionalparent: string | URL

      Returns string

    • Parameters

      • moduleId: string
      • Optionalparent: string

      Returns string

      Use require.resolve or Bun.resolveSync(moduleId, path.dirname(parent)) instead

      Resolve a module ID the same as if you imported it

      The parent argument is optional, and defaults to the current module's path.