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

An exception for cjs config files in a ES modules environment #80

Open
CxRes opened this issue Jul 14, 2020 · 4 comments
Open

An exception for cjs config files in a ES modules environment #80

CxRes opened this issue Jul 14, 2020 · 4 comments

Comments

@CxRes
Copy link

CxRes commented Jul 14, 2020

The dream would be to use ES modules with "type": "modules" for everything in my project, allowing one to go cold turkey on cjs. However, a number of tools, such as, ESLint require cjs config files. This results in the annoying situation where either:

  • I run my config files through a transformer (yuck!).
    or
  • I need to define a '.cjs' es5 config file and manually point every tool to this file because:
    • using .js file with cjs syntax will throw when "type": "modules" is enabled.
    • tools afaik are not configured to read .cjs extension by default.

This imho just another form of unnecessary noise due to a absence of conventions and the painful transition process.

The simplest solution, though not ideal, is to relax ES module requirements when running a tool through npx. In the mean time, this group may be able to convince major tools to move to ES6 config files and/or read '.cjs' config by default.

@CxRes
Copy link
Author

CxRes commented Jul 14, 2020

Just another pedantic note: I am building on windows and to ensure cross-platform compatibility I need to detect the OS and configure things accordingly (e.g. on Windows a linter must accept only CRLF but on other platforms only LF). Using a static .json like configuration file is a non-starter.

@coreyfarrell
Copy link
Member

See also nodejs/modules#389 for ES modules team discussion on this.

FYI the current version of eslint supports loading .eslintrc.cjs by default, many other tools have taken similar steps. nyc supports nyc.config.{js,cjs,mjs} and in the case of .js will detect if it should be loaded as an ES module (per "type": "module" being set or not).

For many tools supporting .cjs is easier than supporting load of ES modules as using import() introduces async to the config load process and some tools might not be able to make that change without major/breaking changes. So I think the first step would be identifying which tools support .js config but not .cjs and advocate for them to add .cjs support. Some tools might be able to support ES modules so in that case they could add .mjs support and even detection for the type of a .js file.

@coreyfarrell
Copy link
Member

Additionally see cosmiconfig/cosmiconfig#224 as this is likely to cover many tools.

@ljharb
Copy link
Member

ljharb commented Jul 14, 2020

You can "ditch CJS" by using .mjs also, you don't need type: "module".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants