Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use ESModules instead of module.exports (#786) #852

Closed
wants to merge 6 commits into from
Closed

Use ESModules instead of module.exports (#786) #852

wants to merge 6 commits into from

Conversation

Konakona-chan
Copy link

Hello there.

As for 2021, NodeJS versions <12 without ESM support (are deprecated)[https://nodejs.org/en/about/releases/].

This PR switches debug package from CJS to ESM version, so it can be used in modern applications based on asynchronous execution.

Resolves #786.

Implementation notes:

  1. This changeset updates dependencies to the latest ones to reduce package fragmentation and introduce ESM-based dependencies
  2. It removes debug.destroy() function according to deprecation notice (notably, it updates ms to canary version from ESM version vercel/ms#159)
  3. It does not change API for existing CJS users who already used import statement; however require(...) should be replaced to import as described in README.md

@Qix-
Copy link
Member

Qix- commented Oct 14, 2021

This package is going to be rewritten in the next version, so there's not much I can do with this PR as it stands. Thank you for taking the time, however!

@Qix- Qix- closed this Oct 14, 2021
@Konakona-chan
Copy link
Author

Cool, I hope it will be released soon!

@milahu
Copy link

milahu commented Nov 10, 2021

@Konakona-chan thanks for your work <3
debug-cjs breaks vite, so i need debug-esm

Uncaught ReferenceError: process is not defined

in node.js line 118

export const inspectOpts = Object.keys(process.env).filter(key => {

this happens in browser, so node.js should not be loaded ...

debug is used here

possible solutions

nope
-import createDebug from 'debug'
+import createDebug from 'debug/src/browser.js'

... or

-import createDebug from 'debug'
+import createDebug from 'debug/browser'

... or in debug/index.js, use dynamic import like

const createDebug = isBrowser
  ? (await import('./browser.js'))
  : (await import('./node.js'))
;

export default createDebug;

debug/package.json

  "exports": {
    ".": {
      "import": {
        "browser": "./src/browser.js",
        "default": "./src/index.js"
      }
    },
    "./package.json": "./package.json"
  },

per npm docs should be

  "exports": {
    ".": {
      "import": {
        "node": "./src/node.js",
        "default": "./src/browser.js"
      }
    },
    "./package.json": "./package.json"
  },

works with rollup : )

fixed in https://github.com/milahu/debug/commits/patch-1

install with

npm install "github:milahu/debug#28e27c8b4249fec2f778cf61f13e18e8c814822a"

to clarify, this is an actual working ESM version of the NPM debug package (what a name ...)

→ no need to wait for

This package is going to be rewritten in the next version

@Konakona-chan
Copy link
Author

@milahu , I was able to overcome the issue with Svelte+Vite and unified (esm-only) -> markdown -> debug (cjs-only) with optimizeDeps:
https://gitlab.com/Kona-chan/kona-chan.gitlab.io/-/blob/master/svelte.config.js#L32

@FossPrime
Copy link

Please resubmit this.

Deno and UNPKG with ?module are broken because of this.

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

Successfully merging this pull request may close these issues.

Use ESModules instead of module.exports
4 participants