Open
Description
minimal-test.mjs
#!/usr/bin/env node
/**
* Minimal reproducible sample for dynamic module loading
*/
import { fileURLToPath } from 'url';
import path from 'path';
// Dynamically load use-m
const { use } = eval(
await fetch('https://unpkg.com/use-m/use.js').then(u => u.text())
);
// Load environment variables from .env
const { config } = await use('[email protected]');
config({ path: path.resolve(process.cwd(), '.env') });
const __filename = fileURLToPath(import.meta.url);
console.log('Current file name:', __filename);
Output
<anonymous_script>:183
if (!scriptPath && typeof __filename !== 'undefined') {
^
ReferenceError: Cannot access '__filename' before initialization
at makeUse (eval at <anonymous> (file:///Users/konard/Code/konard/kaiten-api-tools/minimal-test.mjs:9:17), <anonymous>:183:3)
at use (eval at <anonymous> (file:///Users/konard/Code/konard/kaiten-api-tools/minimal-test.mjs:9:17), <anonymous>:214:19)
at file:///Users/konard/Code/konard/kaiten-api-tools/minimal-test.mjs:14:26
at process.processTicksAndRejections (node:internal/process/task_queues:105:5)
Node.js v22.15.0
Workaround 1
Remove declaration of __filename
that goes after use
initialization.
Workaround 2
Move declaration of __filename
before use
initialization.