import.meta
API
Deno supports a number of properties and methods on the
import.meta
API:
import.meta.url
Returns the URL of the current module.
// main.ts
console.log(import.meta.url);
$ deno run main.ts
file:///dev/main.ts
$ deno run https:/example.com/main.ts
https://example.com/main.ts
import.meta.main
Returns whether the current module is the entry point to your program.
// main.ts
import "./other.ts";
console.log(`Is ${import.meta.url} the main module?`, import.meta.main);
// other.ts
console.log(`Is ${import.meta.url} the main module?`, import.meta.main);
$ deno run main.ts
Is file:///dev/other.ts the main module? false
Is file:///dev/main.ts the main module? true
import.meta.filename
This property is only available for local modules (module that have
file:///...
specifier) and returns undefined
for remote modules.
Returns the fully resolved path to the current module. The value contains OS specific path separators.
// main.ts
console.log(import.meta.filename);
On Unix:
$ deno run main.ts
/dev/main.ts
$ deno run https://example.com/main.ts
undefined
On Windows:
$ deno run main.ts
C:\dev\main.ts
$ deno run https://example.com/main.ts
undefined
import.meta.dirname
This property is only available for local modules (module that have
file:///...
specifier) and returns undefined
for remote modules.
Returns the fully resolved path to the directory containing the current module. The value contains OS specific path separators.
// main.ts
console.log(import.meta.dirname);
On Unix:
$ deno run main.ts
/dev/
$ deno run https://example.com/main.ts
undefined
On Windows:
$ deno run main.ts
C:\dev\
$ deno run https://example.com/main.ts
undefined
import.meta.resolve
Resolve specifiers relative to the current module.
const worker = new Worker(import.meta.resolve("./worker.ts"));
The import.meta.resolve
API takes into account the currently applied import
map, which gives you the ability to resolve "bare" specifiers as well.
With such import map loaded...
{
"imports": {
"fresh": "https://deno.land/x/fresh@1.0.1/dev.ts"
}
}
...you can now resolve:
// resolve.js
console.log(import.meta.resolve("fresh"));
$ deno run resolve.js
https://deno.land/x/fresh@1.0.1/dev.ts