Skip to content

Instantly share code, notes, and snippets.

@jens1101
Last active March 23, 2023 09:11
Show Gist options
  • Save jens1101/547b90bcb5aa98b9709cdb083a5faef8 to your computer and use it in GitHub Desktop.
Save jens1101/547b90bcb5aa98b9709cdb083a5faef8 to your computer and use it in GitHub Desktop.
Common `import.meta.url` use cases
import { fileURLToPath } from "url";
import { dirname } from "path";
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const config = await import(new URL("config.js", import.meta.url));
import { readFile } from "fs/promises";
const fileUrl = new URL('./package.json', import.meta.url);
const fileContents = await readFile(fileUrl, { encoding: 'utf8' });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment