Open utterances-bot opened 2 years ago
What’s the use-case for inport.meta.url
in the browser? Why would a script need to know its absolute path?
What’s the use-case for
inport.meta.url
in the browser? Why would a script need to know its absolute path?
Well, any use case that requires you to know the script URL. You'll know it when you need it. ;)
For example, you'd make the module work in dev mode if it's located on localhost
, and production mode otherwise.
Should note, this is the standard means for determining paths in Deno as well.
@simevidas in terms of why you might need to know this, my most common use case is passing paths to external programs or scripts. I've started favoring Deno for orchestration and automation scripts (as there's no need for npm ci
to run said scripts.
example:
#!/usr/bin/env -S deno run --quiet --allow-run --allow-env --allow-read=./ --allow-write=./ --
import * as log from "https://deno.land/std@0.113.0/log/mod.ts";
const __filename = new URL('', import.meta.url).pathname;
// Will contain trailing slash
const __dirname = new URL('.', import.meta.url).pathname;
log.info({__dirname, __filename});
How to Access ES Module Metadata using import.meta
How to access the meta information (module URL, etc.) of an ES module in JavaScript.
https://dmitripavlutin.com/javascript-import-meta/