question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

esm and cjs config file support

See original GitHub issue

use prettier.config.cjs and prettier.config.mjs for config file.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:8
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

5reactions
SMotaalcommented, Nov 25, 2019

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 the package.type field — and so to close the gap for tools expecting CommonJS configuration in a package.type = "module" (ie ESM) the .cjs extension may be necessary when relying on require(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 the module.exports for that file.

Hope this helps.

2reactions
akauppicommented, Dec 16, 2020

Situation in Dec 2020 (prettier 2.2.1):

One can use a .cjs configuration file in package.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:

$ npm run _format.js
...
> prettier --config ./prettier.config.cjs --write ./src/**/*.js ./app/**/*.js

[error] Invalid configuration file `src/Draggable/draggableX.js`: Unexpected identifier
npm ERR! code 2
npm ERR! path /Users/.../Git/svelte-with-SVG
npm ERR! command failed
npm ERR! command sh -c prettier --config ./prettier.config.cjs --write ./src/**/*.js ./app/**/*.js

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/.../.npm/_logs/2020-12-16T10_14_00_410Z-debug.log

I would expect to rather get an error stating that the config file prettier.config.cjs is bad.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found