You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Webpack v4+ prefers the module field from package.json over the main field even when the webpack target is node.
Since the main module exports {parse, stringify} and the module module exports {default: {parse, stringify}}, this creates an issue when bundling with webpack.
changed the title [-]json5 distrubte e "module": "dist/index.mjs", hurt webpack[/-][+]json5 distrubte "module": "dist/index.mjs", hurt webpack[/+]on Oct 10, 2020
@jordanbtucker possibly, and it may need types - but I wouldn't bother it'll change again with the next typescript / node / bundler / linter / ... sigh
I wonder if copying index.d.ts into node_modules/json5/dist will resolve that error. If so, that would be an easy short term fix that I could publish. It might need to be renamed to index.mjs.d.ts.
I checked. It's not that simple. index.d.ts uses export {...} while index.mjs uses export default, which is the original point of this issue. Plus, index.d.ts imports ./parse.d.ts and ./stringify.d.ts so those paths don't resolve correctly.
The following declaration placed at dist/index.mjs.d.ts works though (no need for @types):
Is there a specific reason you need the ESM version in TypeScript?
NB the current "browser" looks wrong anyway = points to the bundled CJS code.
Both dist/index.js and dist/index.mjs are bundled from the CJS code, but dist/index.js is not CJS code, it's UMD code. If you want to import json5 inside <script type="module"> in the browser, then you would use dist/index.mjs.
<!-- Import json5 as a UMD global --><scriptsrc="https://unpkg.com/json5@2/dist/index.js"></script><script>// JSON5 is a globalconsole.log(JSON5.parse(`{hello: 'world'}`))</script>
or
<!-- Import json5 as a module --><scripttype="module">importJSON5from'https://unpkg.com/json5@2/dist/index.mjs'console.log(JSON5.parse(`{hello: 'world'}`))</script>
I don't want to keep wasting your time with this stuff. Thanks for all the responses.
Is there a specific reason you need the ESM version in TypeScript?
Nope. I'm going through a painful exercise of porting my codebase from cjs to esm. One of the changes is to resolve how the 3rd party packages that I'm using have implemented esm. Of the 50+ packages I'm using I picked json5 to single step into and realised that the code tranformation suggested by node (i.e. import json5 from "json5"; const { parse } = json5;) wasn't doing what I thought it would do - i.e. using the esm code. That's the reason why I wound up here. Whether it would have made a difference I don't know - it was just unexpected - so I dug a bit deeper.
I'm totally happy with the import json5 from "json5/dist/index.mjs" + ts4.5+ solution.
Passing { named: ["parse", "stringify"] } directly to the commonjs() plugin had no effect.
I tried to rewrite the lib directory to ESM (which is good practice these days) and change the Rollup to compile to CJS in a way that stays as compatible for downstream users of the library, but I ran into an issues:
The source imports package.json, which is only possible in portable ESM using import attributes. These are well-supported, but they are new I don't know how comfortable the maintainers would be with this.
Deprecated. In the past, this list has been used to load non-JavaScript modules into Node.js by compiling them on-demand. However, in practice, there are much better ways to do this, such as loading modules via some other Node.js program, or compiling them to JavaScript ahead of time.
Avoid using require.extensions. Use could cause subtle bugs and resolving the extensions gets slower with each registered extension.
I tried taking a stab at modernizing the codebase while preserving functionality for CJS consumers, but the latter looks rather gnarly. It probably requires a CJS-specific workaround to keep working.
For anyone blocked by this: I've published a maintained fork, @bybrave/json5, that fixes it.
Root cause: the sources are CommonJS, and the ESM artifact (module field) was generated by rollup's commonjs plugin, which emits export default {parse, stringify} and no named exports. So import { parse } from 'json5' resolved to undefined under any bundler that prefers module (webpack does, even with target: node), and import/require disagreed on shape.
Fix: the fork is ESM-first with a proper exports map. import JSON5 from '@bybrave/json5', import { parse, stringify } from '@bybrave/json5', and require('@bybrave/json5') all resolve to the same shape (verified from the published tarball). Parser is 100% compatible. Drop-in: npm i @bybrave/json5.
Activity
TrySound commentedon Oct 10, 2020
Using named exports would solve all problems.
lygstate commentedon Oct 10, 2020
There is objection, https://www.bignerdranch.com/blog/default-exports-or-named-exports-why-not-both/
I am bitten by these things.
maybe webpack better to provide a optionMap to config each node module are default export or named export.
lygstate commentedon Oct 10, 2020
Convice me doesn't solve the problem, need convnce everyone and that's not possible.
TrySound commentedon Oct 10, 2020
In this particular case technically these are named exports. There is no "primary" thing here and combining into single object does not solve any problem. If you will need single object use special syntax
import * as JSON5 from 'json5'.https://github.com/json5/json5/blob/master/lib/index.js#L4-L7
lygstate commentedon Oct 10, 2020
These are right, but the generated mjs code are default export
lygstate commentedon Oct 10, 2020
Look at the dist file index.mjs, it's ends with default export, that's truely annoy.
I using
Adjust webpack mainFields: to ['main', 'module'] for supporting newest
json5. to fix. but this is too much
Migrate to named exports and provide node esm support
TrySound commentedon Oct 10, 2020
Check this #241
[-]json5 distrubte e "module": "dist/index.mjs", hurt webpack[/-][+]json5 distrubte "module": "dist/index.mjs", hurt webpack[/+]jordanbtucker commentedon Oct 12, 2020
@lygstate Can you please explain what your issue is rather than just adding a link to an issue in another repo.
TrySound commentedon Oct 12, 2020
The issue is using the library in commonjs code which is bundled by webpack
Though this does not work in node which does not have "default" field.
[-]json5 distrubte "module": "dist/index.mjs", hurt webpack[/-][+]ESM `module` export differs from `main` export[/+]jordanbtucker commentedon Oct 12, 2020
Would it work to add an
exportsfield topackage.jsonas described in Dual CommonJS/ES module packages and Target environment independent packages?TrySound commentedon Oct 12, 2020
It won't work with webpack 4 which supports only "module" field. Do you have any objections to use named exports instead?
https://github.com/json5/json5/pull/241/files
I can migrate whole source to esm in the next PR if you want.
jordanbtucker commentedon Oct 12, 2020
I'm trying to avoid breaking changes.
13 remaining items
ilanc commentedon Feb 25, 2022
@jordanbtucker possibly, and it may need types - but I wouldn't bother it'll change again with the next typescript / node / bundler / linter / ... sigh
ilanc commentedon Feb 25, 2022
@jordanbtucker btw - not sure whether you've read these - but I've found these to be very useful articles on building dual cjs/esm packages:
jordanbtucker commentedon Feb 25, 2022
I checked. It's not that simple.
index.d.tsusesexport {...}whileindex.mjsusesexport default, which is the original point of this issue. Plus,index.d.tsimports./parse.d.tsand./stringify.d.tsso those paths don't resolve correctly.The following declaration placed at
dist/index.mjs.d.tsworks though (no need for@types):Is there a specific reason you need the ESM version in TypeScript?
Both
dist/index.jsanddist/index.mjsare bundled from the CJS code, butdist/index.jsis not CJS code, it's UMD code. If you want to import json5 inside<script type="module">in the browser, then you would usedist/index.mjs.or
ilanc commentedon Feb 25, 2022
I don't want to keep wasting your time with this stuff. Thanks for all the responses.
Nope. I'm going through a painful exercise of porting my codebase from cjs to esm. One of the changes is to resolve how the 3rd party packages that I'm using have implemented esm. Of the 50+ packages I'm using I picked json5 to single step into and realised that the code tranformation suggested by node (i.e.
import json5 from "json5"; const { parse } = json5;) wasn't doing what I thought it would do - i.e. using the esm code. That's the reason why I wound up here. Whether it would have made a difference I don't know - it was just unexpected - so I dug a bit deeper.I'm totally happy with the
import json5 from "json5/dist/index.mjs"+ ts4.5+ solution.fix json5 import json5/json5#240
lisonge commentedon Aug 28, 2024
Due to the lack of proper type declarations in the ESM format
This causes the dts generated by
unplugin-auto-importto not take effectThe solution is shown in the image below

lgarron commentedon Nov 4, 2025
I tried creating a pull request to fix this and failed:
"exports: "named"did not work: https://rollupjs.org/configuration-options/#output-exports{ named: ["parse", "stringify"] }directly to thecommonjs()plugin had no effect.libdirectory to ESM (which is good practice these days) and change the Rollup to compile to CJS in a way that stays as compatible for downstream users of the library, but I ran into an issues:package.json, which is only possible in portable ESM using import attributes. These are well-supported, but they are new I don't know how comfortable the maintainers would be with this.require.extensionsfunctionality ofnode: https://nodejs.org/api/modules.html#requireextensionsI tried taking a stab at modernizing the codebase while preserving functionality for CJS consumers, but the latter looks rather gnarly. It probably requires a CJS-specific workaround to keep working.
Fix named exports for `parse` and `stringify` in the ESM builds.
Fix named exports for `parse` and `stringify` in the ESM builds.
bybraveHQ commentedon Jul 5, 2026
For anyone blocked by this: I've published a maintained fork,
@bybrave/json5, that fixes it.Root cause: the sources are CommonJS, and the ESM artifact (
modulefield) was generated by rollup's commonjs plugin, which emitsexport default {parse, stringify}and no named exports. Soimport { parse } from 'json5'resolved toundefinedunder any bundler that prefersmodule(webpack does, even withtarget: node), andimport/requiredisagreed on shape.Fix: the fork is ESM-first with a proper
exportsmap.import JSON5 from '@bybrave/json5',import { parse, stringify } from '@bybrave/json5', andrequire('@bybrave/json5')all resolve to the same shape (verified from the published tarball). Parser is 100% compatible. Drop-in:npm i @bybrave/json5.feat: esm sources with exports map, esbuild build and bundled types
feat: esm sources with exports map, esbuild build and bundled types
feat: esm sources with exports map, esbuild build and bundled types
ESM-first sources with an exports map, esbuild build, bundled types (…