実行されているファイルのパスを取得する
実行されているファイルのパスを取得するには、import.meta.urlを使用します
https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Statements/import.meta
code:index.ts
const __filename = import.meta.url
// file://home/deno/src/index.ts
__dirnameに相当するディレクトリ名を取得したい場合は、URLを使うと便利です
ref: __dirname
code:ts
const __dirname = new URL("../", import.meta.url).pathname
// /home/deno/src/
ファイルを実行しているカレントディレクトリのパスを取得したい場合はDeno.cwd()を使う
お試しコード
$ deno run --allow-read --reload https://scrapbox.io/api/code/deno-ja/実行されているファイルのパスを取得する/script.js
code:script.js
const path0 = import.meta.url;
const path1 = new URL("../", import.meta.url).pathname;
const path2 = Deno.cwd();
console.log({path0, path1, path2});
// output:
// {
// path0: "https://scrapbox.io/api/code/deno-ja/%E5%AE%9F%E8%A1%8C%E3%81%95%E3%82%8C%E3%81%A6%E3%81%84%E3%82%8B...",
// path1: "/api/code/deno-ja/",
// path2: "このコードを実行したときのcurrent directory path"
// }