-
Notifications
You must be signed in to change notification settings - Fork 30k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add command line argument to force launch node.js file in "ES modules" mode, regardless of extension #41136
Comments
There is a workaround to set Let's create the {
"name": "scripts",
"version": "1.0.0",
"main": "readdir",
"license": "MIT",
"type": "module"
} On launch we got an error:
If we try to rename
But I need to have my |
Because very often in projects we have a So if we force set the |
This is being discussed in #37857. Your current options for an extensionless node executable file are:
Closing as duplicate. |
@aduh95 Thank you for the explanation, can you please tell me more about Do you mean the
But seem it needs Node v17 and even don't work in it:
And "Make an extensionless file a simple proxy to the actual .mjs script" is not the way, because I need to have only the one single file with name "readdir", not the bundle of files ( |
I mean you can provide a loader to customize the behavior of Node.js. You could use a loader that forces the entry point to be parsed as ESM no matter its extension: let entrypoint = true;
export async function load(url, context, defaultLoad) {
if (entrypoint) {
context.format = "module";
entrypoint = false;
}
return defaultLoad(url, context, defaultLoad);
} node --experimental-loader 'data:text/javascript,let%20e=true;export%20function%20load(t,o,r)%7Bif(e)%7Bo.format=%22module%22;e=false%7Dreturn%20r(t,o,r)%7D' readdir Note that this is still experimental, I wouldn't recommend using it for something meant for production. Support on older version of Node.js may vary. |
Is your feature request related to a problem? Please describe.
I have a command line script file, in which I want to use "ES modules" features like top-level await and
import
construction.Let's create the executable
readdir.mjs
script file (using shebang) with this content:And launch it via
./readdir.mjs
console command - it works well, output is:[ 'readdir.mjs' ]
Let's remove the "strange for regular scripts"
.mjs
extension via renaming this file to simplyreaddir
, and launch it via./readdir
- we will got an error:Describe the solution you'd like
We need to have an easy way to use ES modules features in node.js script files regardless of it's extension, like any other language allows!
So it would be good to add some command line argument like
node --type=module
, that will parse the current file as an ES modules type.The text was updated successfully, but these errors were encountered: