ImportMeta

Bun

Symbol

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.

    Does not have 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
    
  • 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; decline; 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.sh/docs/bundler/hmr

  • readonly main: boolean

    Did the current file start 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"
    
  • resolve(specifier: string): string
    resolve(specifier: string, parent?: string | URL): string

    Provides a module-relative resolution function scoped to each module, returning the URL string.

    Second parent parameter is only used when the --experimental-import-meta-resolve command flag enabled.

    @param specifier

    The module specifier to resolve relative to parent.

    @param parent

    The absolute parent module URL to resolve from.

    @returns

    The absolute (file:) URL string for the resolved module.