Skip to content

ESM module export differs from main export #240

Closed

Description

@lygstate

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.

Activity

  1. TrySound commented on Oct 10, 2020

    @TrySound

    Using named exports would solve all problems.

  2. lygstate commented on Oct 10, 2020

    @lygstate
    Author

    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.

  3. lygstate commented on Oct 10, 2020

    @lygstate
    Author

    Using named exports would solve all problems.

    Convice me doesn't solve the problem, need convnce everyone and that's not possible.

  4. TrySound commented on Oct 10, 2020

    @TrySound

    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

  5. lygstate commented on Oct 10, 2020

    @lygstate
    Author

    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

    These are right, but the generated mjs code are default export

  6. lygstate commented on Oct 10, 2020

    @lygstate
    Author

    Look at the dist file index.mjs, it's ends with default export, that's truely annoy.

    const JSON5 = {
        parse,
        stringify,
    };
    
    var lib = JSON5;
    
    export default lib;
    
    

    I using

    Adjust webpack mainFields: to ['main', 'module'] for supporting newest
    json5. to fix. but this is too much

  7. TrySound commented on Oct 10, 2020

    @TrySound

    Check this #241

  8. changed the title [-]json5 distrubte e "module": "dist/index.mjs", hurt webpack[/-] [+]json5 distrubte "module": "dist/index.mjs", hurt webpack[/+] on Oct 10, 2020
  9. jordanbtucker commented on Oct 12, 2020

    @jordanbtucker
    Member

    @lygstate Can you please explain what your issue is rather than just adding a link to an issue in another repo.

  10. TrySound commented on Oct 12, 2020

    @TrySound

    The issue is using the library in commonjs code which is bundled by webpack

    // webpack resolves json5 with module field out of the box which lead to this usage
    const json5 = require('json5').default
    

    Though this does not work in node which does not have "default" field.

  11. changed the title [-]json5 distrubte "module": "dist/index.mjs", hurt webpack[/-] [+]ESM `module` export differs from `main` export[/+] on Oct 12, 2020
  12. jordanbtucker commented on Oct 12, 2020

    @jordanbtucker
    Member

    Would it work to add an exports field to package.json as described in Dual CommonJS/ES module packages and Target environment independent packages?

  13. TrySound commented on Oct 12, 2020

    @TrySound

    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.

  14. jordanbtucker commented on Oct 12, 2020

    @jordanbtucker
    Member

    I'm trying to avoid breaking changes.

  15. 13 remaining items

  16. ilanc commented on Feb 25, 2022

    @ilanc

    @jordanbtucker possibly, and it may need types - but I wouldn't bother it'll change again with the next typescript / node / bundler / linter / ... sigh

  17. jordanbtucker commented on Feb 25, 2022

    @jordanbtucker
    Member

    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):

    import parse = require('../lib/parse')
    import stringify = require('../lib/stringify')
    
    interface JSON5 {
      parse: typeof parse
      stringify: typeof stringify
    }
    
    declare const JSON5: JSON5
    export default JSON5

    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 -->
    <script src="https://unpkg.com/json5@2/dist/index.js"></script>
    <script>
    // JSON5 is a global
    console.log(JSON5.parse(`{hello: 'world'}`))
    </script>

    or

    <!-- Import json5 as a module -->
    <script type="module">
    import JSON5 from 'https://unpkg.com/json5@2/dist/index.mjs'
    console.log(JSON5.parse(`{hello: 'world'}`))
    </script>
  18. ilanc commented on Feb 25, 2022

    @ilanc

    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.

  19. lisonge commented on Aug 28, 2024

    @lisonge

    Due to the lack of proper type declarations in the ESM format

    This causes the dts generated by unplugin-auto-import to not take effect

    image image

    The solution is shown in the image below
    image

  20. lgarron commented on Nov 4, 2025

    @lgarron

    I tried creating a pull request to fix this and failed:

    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.

  21. bybraveHQ commented on Jul 5, 2026

    @bybraveHQ
    Collaborator

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions