Skip to content

Conversation

@joshkel
Copy link
Contributor

@joshkel joshkel commented Aug 6, 2025

Node.js handles ESM-CJS interoperability by using its cjs-module-lexer to parse CJS files and turn their exports into named exports. cjs-module-lexer is deliberately limited in its capabilities. Switching export styles makes jsonfile's exports recognizable by cjs-module-lexer so that ESM code such aborts

import { readFileSync } from 'jsonfile'

can work as expected, without having to change jsonfile itself to ESM.

Fixes #153

Node.js handles ESM-CJS interoperability by using its [cjs-module-lexer](https://github.com/nodejs/cjs-module-lexer) to parse CJS files and turn their exports into named exports.  cjs-module-lexer is deliberately limited in its capabilities.  Switching export styles makes jsonfile's exports recognizable by cjs-module-lexer so that ESM code such aborts

```
import { readFileSync } from 'jsonfile'
```

can work as expected, without having to change jsonfile itself to ESM.

Fixes #153
@joshkel
Copy link
Contributor Author

joshkel commented Aug 6, 2025

To demonstrate / test this locally, create a file, test.mjs:

import { readFileSync } from './index.js';
console.log(readFileSync('./package.json'));

Then run it with and without this change.

Copy link
Collaborator

@RyanZim RyanZim left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@RyanZim RyanZim merged commit 9687957 into jprichardson:master Aug 9, 2025
5 checks passed
@RyanZim
Copy link
Collaborator

RyanZim commented Aug 9, 2025

Thanks @joshkel! Will try to release sometime next week (I don't like releasing popular libraries on weekends).

@RyanZim
Copy link
Collaborator

RyanZim commented Aug 12, 2025

Published as v6.2.0 🎉

@joshkel
Copy link
Contributor Author

joshkel commented Aug 12, 2025

Thanks, @RyanZim!

@joshkel joshkel deleted the esm-compatibility branch August 12, 2025 21:14
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

Successfully merging this pull request may close these issues.

Breakage when running as ESM

2 participants