ImportMeta - Web documentation
interface ImportMeta

Deno provides extra properties on import.meta. These are included here to ensure that these are still available when using the Deno namespace in conjunction with other type libs, like dom.

Properties

url: string

A string representation of the fully qualified module URL. When the module is loaded locally, the value will be a file URL (e.g. file:///path/module.ts).

You can also parse the string as a URL to determine more information about how the current module was loaded. For example to determine if a module was local or not:

const url = new URL(import.meta.url);
if (url.protocol === "file:") {
  console.log("this module was loaded locally");
}
optional
filename: string

The absolute path of the current module.

This property is only provided for local modules (ie. using file:// URLs).

Example:

// Unix
console.log(import.meta.filename); // /home/alice/my_module.ts

// Windows
console.log(import.meta.filename); // C:\alice\my_module.ts
optional
dirname: string

The absolute path of the directory containing the current module.

This property is only provided for local modules (ie. using file:// URLs).

  • Example:
// Unix
console.log(import.meta.dirname); // /home/alice

// Windows
console.log(import.meta.dirname); // C:\alice
main: boolean

A flag that indicates if the current module is the main module that was called when starting the program under Deno.

if (import.meta.main) {
  // this was loaded as the main module, maybe do some bootstrapping
}

Methods

resolve(specifier: string): string

A function that returns resolved specifier as if it would be imported using import(specifier).

console.log(import.meta.resolve("./foo.js"));
// file:///dev/foo.js