esm and cjs config file support
See original GitHub issueuse prettier.config.cjs
and prettier.config.mjs
for config file.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:8
- Comments:5 (2 by maintainers)
Top Results From Across the Web
Publish ESM and CJS in a single package - Anthony Fu
A short tutorial of shipping both ESM and CJS dual formats in a single NPM package.
Read more >Documentation - ECMAScript Modules in Node.js - TypeScript
js has been working to support running ECMAScript modules (ESM). This has been a very difficult feature to support, since the foundation of...
Read more >Migrating an NPM package to use ES Modules - Medium
Unfortunately ESLint doesn't yet support an ESM config file. So here's what you need to do: Rename .eslintrc.js to .eslintrc.cjs; Add sourceType: ' ......
Read more >Adam Wathan on Twitter: "I can't seem to get `postcss.config.js ...
... in ESM Next projects If I use `postcss.config.cjs` the file is ... Then there's .mjs support so I'll recommend trying that too....
Read more >pkgroll - npm
TypeScript friendly Transform TS and emit .d.ts files (Supports .cts / .mts too!) Node.js ESM ⇄ CJS friendly Preserves named exports from ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Glad to see
.cjs
and.mjs
out here.This comes from Node.js finally unflagging ESM (ie
--experimental-modules
).The Node.js Modules team took time to explore matters of source disambiguation where unlike browsers that only ever deal with
text/javascript
files based on clear intent of loading, tooling and Node.js itself cannot always assume the format of.js
files.Package authors can indicate that
.js
is either ESM of CommonJS using thepackage.type
field — and so to close the gap for tools expecting CommonJS configuration in apackage.type = "module"
(ie ESM) the.cjs
extension may be necessary when relying onrequire(configuration)
in their implementation.The reverse can be accomplished with
.mjs
.Worth noting that the limitation only applies to
require
— where it cannot at least for now seamlessly load from ESM — but CommonJS and ESM code can always(await import('…/configuration.cjs')).default
to return themodule.exports
for that file.Hope this helps.
Situation in Dec 2020 (prettier 2.2.1):
One can use a
.cjs
configuration file inpackage.type = "module"
project. Plain.js
does not work which is pretty common still these days.Beware of this: (could deserve to be a separate issue, but no time, sorry…)
If there is a syntax error in the JavaScript configuration file (
prettier.config.cjs
),prettier
seems to ignore the file and try looking for something else. My error message was:I would expect to rather get an error stating that the config file
prettier.config.cjs
is bad.