Skip to content
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

Allow Setting Extension Priority (and Then Not Requiring Extensions in Imports) #56660

Open
machineghost opened this issue Jan 19, 2025 · 4 comments
Labels
feature request Issues that request new features to be added to Node.js.

Comments

@machineghost
Copy link

machineghost commented Jan 19, 2025

What is the problem this feature will solve?

In recent years Node has been trading developer convenience for functionality, as several recent changes (eg. https://satanacchio.hashnode.dev/everything-you-need-to-know-about-nodejs-type-stripping#heading-a-predictable-experience) strongly encourage:

import foo from 'foo.js';

over:

import foo from 'foo';

However, for most devs that's a false trade! We can have convenient (extension-less) imports and have the benefits of the new features.

How?

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

node --prioritizeExtensions=js,jsx,ts,tsx

Now, when Node sees:

import foo from 'foo';

it will treat it as

import foo from 'foo.js';

... unless that fails, in which case it will treat is as:

import foo from 'foo.jsx';

... and so on.

What alternatives have you considered?

Not requiring an argument ... but the dev needs some way to specify "if you see foo.js and foo.ts, but I said from foo, use the .js one".

Look, I know three or four extra characters of typing (.jsx) may not seem like a lot, but it adds up across dozens of imports in hundreds (or thousands) of files. Please, consider letting us have our extension-less imports back.

@machineghost machineghost added the feature request Issues that request new features to be added to Node.js. label Jan 19, 2025
@github-project-automation github-project-automation bot moved this to Awaiting Triage in Node.js feature requests Jan 19, 2025
@marco-ippolito
Copy link
Member

marco-ippolito commented Jan 21, 2025

You example is not correct:

import foo from 'foo'

Will look for a package foo in the node_modules.
While:

import foo from './foo'

is not allowed.

I'm -1 on having extension guessing.
Node.js would have to do several attempts to read on the fs. A few more characters would save the runtime a lot of overhead which is reasonable.
Also the list would be:
js, cjs, mjs, ts, cts, mts which is quite long.

@machineghost
Copy link
Author

You example is not correct:

Sorry, the perils of pseudocode ... but you seemed to grok my intent :)

Node.js would have to do several attempts to read on the fs. A few more characters would save the runtime a lot of overhead which is reasonable.

You seem to be making assumptions for every Node developer. Personally, and I suspect a lot of people would share this preference, I would happily trade 0.5s startup time for never having to write an extension in my imports ever again.

@marco-ippolito
Copy link
Member

I mean if it stays behind a flag, I'd not be totally opposed. I would not like to be the default behavior. I'm afraid it will create a flavor that only works for some and not for others.

@machineghost
Copy link
Author

I'm afraid it will create a flavor that only works for some and not for others.

Legitimate concern, but my counterarguments would be:

  • every new flag does that (to an extent)
  • if someone picks the "wrong" flavor and wants to switch, they're probably just a multi-file find/replace or two away from the other flavor
  • Node already has "mixed flavors" (both imports with extensions and ones without)

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
Status: Awaiting Triage
Development

No branches or pull requests

2 participants