nodejs / modules

Node.js Modules Team
MIT License
413 stars 42 forks source link

`import.meta.url` is `undefined` inside module file. #576

Closed trusktr closed 1 year ago

trusktr commented 1 year ago

I'm running a file like this:

node --input-type module --experimental-import-meta-resolve < ./dev-server.js

And inside that file we have this:

console.log(import.meta.url)
console.log(import.meta.resolve)
process.exit()

and this is the output:

undefined
undefined
trusktr commented 1 year ago

Oh, that makes sense, because it is piped into the command, Node has no idea what the URL is.

If I rename it to .mjs and run it like so it works:

node --experimental-import-meta-resolve ./dev-server.mjs

I must be missing something simple. How do we run a regular .js file as esm?

GeoffreyBooth commented 1 year ago

I must be missing something simple. How do we run a regular .js file as esm?

Add "type": "module" to the nearest parent package.json. If you have further questions please use https://github.com/nodejs/help, this repo is for the team developing the modules implementation.

trusktr commented 1 year ago

It'd be great if node --input-type module ./dev-server.js just worked, as intuition would expect.