tooling
tooling copied to clipboard
An exception for cjs config files in a ES modules environment
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
.jsfile with cjs syntax will throw when"type": "modules"is enabled. - tools afaik are not configured to read
.cjsextension by default.
- using
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.
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.
See also https://github.com/nodejs/modules/issues/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.
Additionally see https://github.com/davidtheclark/cosmiconfig/issues/224 as this is likely to cover many tools.
You can "ditch CJS" by using .mjs also, you don't need type: "module".