Description
- Version: 16.1.0
- Platform: Linux
- Subsystem:
What steps will reproduce the bug?
I write TypeScript which Node doesn't read so I write esbuild thefile.ts | node --input-type=module -
to strip the TS typings and pass the ESM directly to Node. This works 💯👍
However, if the code uses import.meta.url
such as const __dirname = path.dirname(fileURLToPath(import.meta.url));
this throws:
node:internal/process/esm_loader:74
internalBinding('errors').triggerUncaughtException(
^
TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string or an instance of URL. Received undefined
at new NodeError (node:internal/errors:363:5)
at fileURLToPath (node:internal/url:1372:11)
at file:///home/today/_/work/acorn-macros/test/style.macro/[eval1]:12:32
at ModuleJob.run (node:internal/modules/esm/module_job:175:25)
at async Loader.eval (node:internal/modules/esm/loader:170:24)
at async node:internal/process/execution:49:24
at async loadESM (node:internal/process/esm_loader:68:5) {
code: 'ERR_INVALID_ARG_TYPE'
}
Because import.meta.url
is undefined. Shouldn't it be "file:///home/today/_/work/acorn-macros/test/style.macro/[eval1]" since that's what's shown in the stacktrace?
How often does it reproduce? Is there a required condition?
Using import.meta.url
when accepting a script via stdin.
> echo "console.log(import.meta.url)" | node --input-type=module
undefined
What is the expected behavior?
The url
property is "file:///home/today/_/work/acorn-macros/test/style.macro/[eval1]"
> echo "console.log(import.meta.url)" | node --input-type=module
file:///the/real/path/[eval1]
As a workaround you can hack this in via:
if (import.meta.url === undefined) {
try {
throw new Error();
} catch (e) {
const url = /at (.+):\d+:\d+$/.exec(e.stack.split('\n')[1])[1];
console.log('Reset to', url); // Logs "Reset to file:///home/today/_/work/acorn-macros/test/style.macro/[eval1]"
import.meta.url = url;
}
}
// ... now carry on as normal, no errors:
const __dirname = path.dirname(fileURLToPath(import.meta.url));
What do you see instead?
Throws :(
Additional information
Thank you for maintaining a wonderful project and doing all the work to make ESM in Node a success! ✨