Skip to content

Make --input-type=module work in general #44457

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

Closed
Pomax opened this issue Aug 30, 2022 · 5 comments
Closed

Make --input-type=module work in general #44457

Pomax opened this issue Aug 30, 2022 · 5 comments
Labels
feature request Issues that request new features to be added to Node.js.

Comments

@Pomax
Copy link

Pomax commented Aug 30, 2022

What is the problem this feature will solve?

Node cannot be used for one-time runs of ESM code in "no package.json" context or in CJS projects, or conversely in CJS mode in ESM projects, because --input-type is ignored when node is told to run a file rather than a string.

This includes not being able to run third party scripts in a project that has been written in "the other flavour" without transpiling, which makes very little sense when Node knows how to run both flavours of JS just fine, and just has no way to make it do so on a per-run basis.

What is the feature you are proposing to solve the problem?

Updating --input-type to apply to both strings and files, so that node --input-type=module onetimescript.js runs in ESM mode for that single run would be incredibly useful.

What alternatives have you considered?

There are no alternatives for telling Node which mode to run in on a per-run basis.

@Pomax Pomax added the feature request Issues that request new features to be added to Node.js. label Aug 30, 2022
@vorbrodt
Copy link

Do you think this issue could be suitable for a new contributor? If so, I would like to be assigned to it and give it a try.

@aduh95
Copy link
Contributor

aduh95 commented Sep 12, 2022

Duplicate of https://github.com/nodejs/node/discussions/37857. The TL;DR I think can be summarise by:

  • there's support for such a flag.
  • it would probably involve loaders (actually it's already feasible with node --experimental-loader 'data:text/javascript,let entryPoint=true;export async function resolve(_,__,next){if(entryPoint){entryPoint=false;return {...(await next(...arguments)),format:"module"};}return next(...arguments)}' onetimescript.js).
  • there's also arguments for not making this too much easier, as having well-defined rules on how to detect if a file should be treated as ESM or CJS, and having to create echo '{"type":"module"}' > package.json is also a workaround.

I'm going to close this to keep the discussion in the other thread, feel free to continue there.

@aduh95 aduh95 closed this as not planned Won't fix, can't repro, duplicate, stale Sep 12, 2022
@Pomax
Copy link
Author

Pomax commented Sep 13, 2022

That thread has heavily derailed with massive scope creep including things like "what to do when there is no extension", "adding a completely different --package flag", etc. etc.

This is a very clear, well scoped request to extend --input-type to not just strings, but also files. The end. No scope creep, no "it'd be cool if we also", just a simple feature request to get a flag that already works, work better.

@al6x
Copy link

al6x commented Oct 1, 2023

Node.JS is currently unable to execute standard JS.
Without creating package.json or nonsense .mjs extension.

@sbc100
Copy link

sbc100 commented May 5, 2025

The link above seems to be completely missing (404): https://github.com/nodejs/node/discussions/37857

Can we re-open this issue perhaps? In emscripten it would be very useful for building command line JS applications that can be run via #! line without creating dummy package.json files alongside each of them. See emscripten-core/emscripten#24244

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request Issues that request new features to be added to Node.js.
Projects
None yet
Development

No branches or pull requests

5 participants