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
Bug: [flat config] .mjs
extension for config file
#16580
Comments
.mjs
extension for config file.mjs
extension for config file
Hi @dburles, thanks for the issue! This is not a bug. ESLint looks for an
We want to support both CJS and ESM formats for eslint config files, so supporting only Since this is a bug report, I'm not sure if you're asking for ESLint to support ESM configs in commons js projects, or it wasn't clear that in commonjs projects |
fwiw we actually write our source as modules (typescript) and our build target is commonjs. because of this, we'd naturally also want our eslint config to be an es module (since the rest of our sources are technically). i think for that reason, it'd be a good idea to detect the multiple extensions and leave the choice up to the consumer here. |
This comment was marked as spam.
This comment was marked as spam.
This comment was marked as spam.
This comment was marked as spam.
Thanks @mdjermanovic, a bit of both, it wasn't super clear that the config should be in the same format as the project I assumed the new config format was ESM only, since that's all I saw in the documentation, with no mention of commonjs.
I think the best solution for now, if commonjs configs are to be supported, is for ESlint config to follow Node with how it handles file extensions. As @43081j mentioned, supporting There are plenty of benefits to ESM only config regardless of the project type, perhaps it's something that will be considered along with the potential ESlint rewrite (See "Complete rewrite of ESLint" discussion #16557). |
I think the documentation should be updated to clarify this.
Makes sense to me. Config file search could first check if @nzakas what do you think? |
I agree with updating the documentation. Because most JavaScript is now being written ESM-first, it makes sense that the bulk of the docs be written with that in mind. Also, because this is still an experimental feature, the docs are still WIP as well. I disagree with supporting other extensions. The intent is to have just one configuration file to avoid needing to check for multiple different files, which is what eslintrc does. Adding in support for .mjs and .cjs just adds complexity with very little tangible value. This was the mistake we made with eslintrc: trying to accommodate everyone’s preferences for config files, and I don’t want to repeat that. |
Most JavaScript is absolutely being authored in ESM, but the vast majority of that is transpiled to CJS, since native ESM has few advantages and many disadvantages over CJS output. I do, however, agree that eslint configs should be able to have the cjs or mjs extensions, because that’s what node defines as the expected working extensions. |
That makes sense, the docs just need to make it perfectly clear that the configuration file format follows your project.
That's a very valid downside, the advantage as @ljharb also mentioned is that it follows the Node convention which is perhaps a bit less confusing to users, and gives them the ability to decide what they would prefer to use in their projects. I don't hold a strong position either way, it seems like an ESM only config might be too early, which is one way to address this problem, since ESM can of course still import commonjs. However perhaps that would be an easier transition with the ESM rewrite as it would of course be required. |
It is definitely too early to enforce ESM-only configs, which is why I find the current If anyone wants to take a stab at updating the docs, I'd appreciate it. |
Part of the point is our entire project is written in esm but outputs commonjs on build. My devs would naturally want to write this config the same way as the rest of the codebase: esm. Which is why the mjs/cjs extensions node defines would be useful. However if you're strongly against that, being very clear in the docs that it follows your package |
If your project does not have a package If in the future ESLint decides to support only one config file name and format that any project could use, it would be |
I’m not 100% sure, but I think if you pass |
A workaround had to do was create a config, publish to npm, and use a plain json config with "extends", no more trying to wrestle with the errors. |
Unfortunately no, it attempts to parse it as YAML. |
@dburles you'll need to use the environment variable to force the file to be interpreted as flat config. |
Whoops! that does it, thanks. That's a decent workaround. |
Oops! It looks like we lost track of this issue. What do we want to do here? This issue will auto-close in 7 days without an update. |
In the 2022-12-01 TSC Meeting, we decided not to support .cjs and .mjs config files. I've opened #16864 to discuss async function configs as a way to address #16580 (comment). Marking this issue as accepted to update Configuration Files (New) docs with the CJS config format for |
fwiw the arguments against it are incredibly weak.
as specified in the docs: so @nzakas you're worried implementing the same strategy as everyone else (incl node itself) will lead to the convoluted mess you had before. that'll only happen if you decide to build your own strategy on top of it, which you 100% shouldn't do (a team has already gone to a lot of effort to create this well defined strategy, just use it as-is). not to sound harsh but you're now the odd one out (eslint). all the other tools are already following the right strategy or have plans to. edit: some quick googling of what already follows this strategy:
|
@43081j thanks for the feedback. I assure you we considered all of the ramifications of this decision. |
@nzakas and your reasoning is still that you're afraid the config will explode like before? or is there some other concern you have? this is a tried and tested, well documented resolution strategy by node itself. if you implement it as-is, no extra behaviours, there's no chance of reintroducing the complexities your old config system had. for sake of visibility, it would be kind of you to explain what the concerns are if not the one you originally stated (also not trying to be overly negative here, just wanting to understand why such a decision would be made. especially in the face of many equally popular tools who already went ahead with it) |
@43081j I've already commented in this issue and the transcripts and minute meetings from the TSC meeting are public and free for you to view. It's not a good use of our limited time as maintainers to re-argue and re-discuss every decision that people don't agree with. You can use the |
If |
100% agree on that I was under the impression even with the c cli switch, it wouldn't work quite right. So sorry I seem to have completely misunderstood that part of it As long as there's some way to specify that eslint explicitly loads a common js module and vice versa (in a package with the opposite type), it seems to be problem solved |
One downside to having to specify the config file is that you'll also need to configure ESLint editor extensions specific to the project, eg. (VSCode) microsoft/vscode-eslint#1518 (comment). |
Please keep in mind that the docs are still very early as this feature is considered experimental. We will be revisiting, revising, and expanding the docs once flat config reaches GA. |
FYI: The workaround combining #16580 (comment) and #16580 (comment) has worked well for me! However, it seems that additional configuration is needed in .vscode/settings.json to allow vscode-eslint to load eslint.config.mjs as well. The workaround that most people need is as follows. // package.json
{
"scripts": {
"lint": "ESLINT_USE_FLAT_CONFIG=true eslint -c eslint.config.mjs ."
}
} // .vscode/settings.json
{
"eslint.experimental.useFlatConfig": true,
"eslint.options": {
"overrideConfigFile": "eslint.config.mjs"
}
} |
The deprecation of However, maintaining support for The flexibility to use |
Thanks, we aren't soliciting additional opinions at this point. |
For the record, this has been addressed in: |
Environment
Environment Info:
Node version: v19.1.0
npm version: v8.19.3
Local ESLint version: v8.28.0 (Currently used)
Global ESLint version: Not found
Operating System: darwin 22.1.0
What parser are you using?
Default (Espree)
What did you do?
My npm package is commonjs, with
.js
extensions, alongside ESM with.mjs
extensions for running tests. Runningnpx eslint .
results in Node failing to parse the configuration file, as it assumes it to be commonjs.What did you expect to happen?
The config file should only support a
.mjs
extension (eslint.config.mjs
) to avoid this problem since Node will always execute it as ESM regardless of thetype
field defined inpackage.json
.What actually happened?
(Shortened)
Participation
Additional comments
No response
The text was updated successfully, but these errors were encountered: