Skip to content

Use ESModules instead of module.exports #786

Description

@jehon

Today, debug use module.exports facility.
But nodejs and browser can support import / export notation, aka es6 modules.
While preparing the v5, why not going into that mode?
#656 ?

Activity

  1. Qix- commented on Oct 6, 2020

    @Qix-
    Member

    It's not supported natively, so a build step would be required. Unfortunately, package management for the different types of modules still is pretty terrible. This is why most large modules are still CJS exported.

  2. jehon commented on Nov 3, 2020

    @jehon
    Author

    You are right...

    Conditional exports be a solution when not experimental anymore, or for next major:

    // package.json
    {
    "main": "./main-require.cjs",
    "exports": {
    "import": "./main-module.js",
    "require": "./main-require.cjs"
    },
    "type": "module"
    }

    https://nodejs.org/dist/latest-v15.x/docs/api/packages.html#packages_conditional_exports

    Storing it here to not loosing this...

  3. tverilytt commented on May 21, 2021

    @tverilytt

    Any news on this topic?

  4. tverilytt commented on May 21, 2021

    @tverilytt

    In Node.js ESM this works for me:

    import debugModule from 'debug';
    const debug = new debugModule('foo');

  5. Qix- commented on May 21, 2021

    @Qix-
    Member

    Yep, v5 will be pure ESM.

  6. jimmywarting commented on Jul 10, 2021

    @jimmywarting

    It would be cool to see debug being a ESM only package, It would put pressure on many packages towards upgrading their commonjs packages to ESM as well and benefit the hole js ecosystem in the end.
    otherwise they can still use a older version of debug

  7. mrmckeb commented on Aug 25, 2021

    @mrmckeb

    Hi @Qix-, we've been working on ESM support for ms, and it would be great to get your feedback if you have some time:
    vercel/ms#163

    This work is also published as 3.0.0-beta.2.

  8. milahu commented on Nov 27, 2021

    @milahu
  9. Qix- commented on Nov 27, 2021

    @Qix-
  10. milahu commented on Nov 27, 2021

    @milahu
  11. Qix- commented on Nov 27, 2021

    @Qix-
  12. deleted a comment from milahu on Nov 27, 2021
  13. lubomir-haralampiev commented on Jan 6, 2022

    @lubomir-haralampiev

    The workaround suggested in the comment above

    In Node.js ESM this works for me:

    import debugModule from 'debug';
    const debug = new debugModule('foo');

    didn't work for me with the version 4.3.3.

    I get the error TypeError: debugModule is not a constructor.

    Generally if I have "type": "module" in my package.json an import results in an empty object {}. Importing it in a cjs module results in a function.

    Any other workaround for usage in an es module? Or do I better wait for the next major release?

  14. Qix- commented on Mar 4, 2022

    @Qix-
    Member
    import makeDebug from 'debug';
    const debug = makeDebug('foo:bar');
    debug.enabled = true;
    debug('hi');
    

    Works fine for me.

  15. jimmywarting commented on Mar 4, 2022

    @jimmywarting

    we should at least discourage this antipattern var debug = require('debug')('http')

    it's bad to call/chain require, it only makes it harder for those who wish to swtich to esm at some point later
    should rather be

    const debugFactory = require('debug')
    const debug = debugFactory('foo:bar')
  16. Qix- commented on Mar 4, 2022

    @Qix-
    Member

    @jimmywarting agreed, PR welcome. That's a good start.

  17. Qix- commented on Mar 8, 2022

    @Qix-
    Member

    Debug is not a class. Don't call it with new.

  18. jmcombs commented on Mar 8, 2022

    @jmcombs

    Debug is not a class. Don't call it with new.

    Thank you, @qix. I deleted my comment since it was incorrect.

  19. dmnsgn commented on May 13, 2022

    @dmnsgn
  20. Qix- commented on May 13, 2022

    @Qix-
  21. webketje commented on Jun 3, 2022

    @webketje

    I've been doing similar research into CJS/ESM for metalsmith and documented findings in this issue. TLDR:

    Tests using a linked metalsmith package on diffferent Node versions with NVM resulted in inconvenient results for either the user (both ESM/CJS) or the maintainer:

    • specifying a package.json exports property will almost certainly result in a semver-major breaking change
    • specifying a shallow index.mjs wrapper and declaring"type": "commonjs" was inconvenient for ESM users as you needed to import from '/index.mjs'
    • declaring "type": "module" would require including a bundler with dual ESM/CJS builds
    • unit testing (with mocha) was a pain

    In the case of debug, potentially even more build outputs would be required to work with native browser imports & bundlers.
    The only "safe" option to support both CJS/ESM with no rewrite required in Node was using a single module.exports = default export as debug does in v4

  22. mariusrak commented on Feb 21, 2024

    @mariusrak

    Not using ESModules is breaking my build. This lib is buried deep in dependency chain of a lib I wanted to use and it is now not usable beucause of the incorrect modules.export usage. So, please fix soon, as this might break down many builds.

  23. amritk commented on Apr 12, 2024

    @amritk
    import makeDebug from 'debug';
    const debug = makeDebug('foo:bar');
    debug.enabled = true;
    debug('hi');
    

    Works fine for me.

    hey @Qix-
    How do you get this to work? I get the following error: x does not provide an export named 'default'

  24. jonkoops commented on Feb 18, 2025

    @jonkoops

    Looks like starting April when Node.js 18 becomes end-of-life it will be possible to move to ECMAScript modules in a backwards compatible manner for all version of Node.js receiving active support. When this time comes I can dedicate effort to help resolve pending issues and assist in the migration.

  25. TheAlexLichter commented on May 24, 2025

    @TheAlexLichter

    require(esm) landed and was backported to Node 20 too! With Node 18 EOL, all existing non-EOL Node versions support it now 👏🏻

  26. productdevbook commented on Jun 19, 2025

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions