Skip to content

ESM-compatible exports - #162

Merged
RyanZim merged 2 commits into
jprichardson:masterfrom
joshkel:esm-compatibility
Aug 9, 2025
Merged

ESM-compatible exports#162
RyanZim merged 2 commits into
jprichardson:masterfrom
joshkel:esm-compatibility

Conversation

@joshkel

@joshkel joshkel commented Aug 6, 2025

Copy link
Copy Markdown
Contributor

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

joshkel commented Aug 6, 2025

Copy link
Copy Markdown
Contributor Author

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.

@RyanZim RyanZim left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM

Comment thread index.js
@RyanZim
RyanZim merged commit 9687957 into jprichardson:master Aug 9, 2025
5 checks passed
@RyanZim

RyanZim commented Aug 9, 2025

Copy link
Copy Markdown
Collaborator

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

@RyanZim

RyanZim commented Aug 12, 2025

Copy link
Copy Markdown
Collaborator

Published as v6.2.0 🎉

@joshkel

joshkel commented Aug 12, 2025

Copy link
Copy Markdown
Contributor Author

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