Skip to content

fix(packaging): add exports map so default import works under Vite/Vitest#1655

Open
MatthewHarrigan wants to merge 1 commit into
auth0:masterfrom
MatthewHarrigan:fix/exports-map
Open

fix(packaging): add exports map so default import works under Vite/Vitest#1655
MatthewHarrigan wants to merge 1 commit into
auth0:masterfrom
MatthewHarrigan:fix/exports-map

Conversation

@MatthewHarrigan

Copy link
Copy Markdown

Fixes #1654.

Changes

Adds an exports field to package.json so each module environment resolves auth0-js to the correct bundle. No code changes; no dist/ rebuild; no build/test infrastructure changes.

"exports": {
  ".": {
    "import":  "./dist/auth0.min.esm.js",
    "require": "./dist/auth0.min.js",
    "default": "./dist/auth0.min.esm.js"
  },
  "./dist/*":        "./dist/*",
  "./build/*":       "./build/*",
  "./package.json":  "./package.json"
}

Why this works:

Honest scope

This PR fixes the ESM consumer path (the bug in #1654 — the one breaking the documented import). The pure-Node CJS require() path remains broken on v10 because "type": "module" causes Node to load .js files as ESM regardless of the require condition target, so the UMD bundle's exports.X = ... writes still have no effect. Fixing that properly requires renaming the UMD output to .cjs (or splitting the build), which is out of scope here. Filing as a separate concern if maintainers want.

Verified on a baseline of v10.0.0 master (pre-PR): require('auth0-js') already returns [Module: null prototype] {} — so this PR neither fixes nor regresses CJS.

References

Testing

All existing local CI gates green against this branch:

Gate Result
npm run lint clean
npm run build produces all 4 expected artifacts
npm test 658 passing
npm run test:es-check:es5 pass
npm run test:es-check:es2015:module pass

I didn't run npm run smoke:modern or the BrowserStack e2e tests — those need toolchain I don't have set up locally. Happy to add coverage if the reviewer suggests a specific gate.

Resolution probe (Node 22, ESM consumer)

Against the unmodified v10.0.0:

default import: undefined
namespace keys: []

Against this branch:

default import: { Authentication: [Function], Management: [Function], WebAuth: [Function], version: '10.0.0' }
default.Authentication: function
default.WebAuth: function
  • This change adds unit test coverage — N/A, packaging-only metadata change
  • This change adds integration test coverage — N/A, packaging-only metadata change

Checklist

@MatthewHarrigan MatthewHarrigan requested a review from a team as a code owner May 20, 2026 09:34
@MatthewHarrigan

Copy link
Copy Markdown
Author

Hi @amitsingh05667 - I've updated the branch

@MatthewHarrigan

Copy link
Copy Markdown
Author

Hi @amitsingh05667 - is it possible to take another look at this if you have time?

@cschetan77

Copy link
Copy Markdown

Hi @MatthewHarrigan, apologies for the late response.
Thanks for fixing this. As you pointed out, the require() path remains broken, the require condition points at auth0.min.js, and with "type": "module" set, Node refuses to load a .js file as CJS. That can be addressed separately.
Looks good to me.
I am going ahead and merging it, will release a patch shortly.

@cschetan77

Copy link
Copy Markdown

@MatthewHarrigan I think there is one commit that doesn't have a verified signature. Could you please sign all the commits and re push them.

@MatthewHarrigan MatthewHarrigan force-pushed the fix/exports-map branch 2 times, most recently from 9b970cd to f7bbe54 Compare June 24, 2026 12:25
@MatthewHarrigan

MatthewHarrigan commented Jun 24, 2026

Copy link
Copy Markdown
Author

Hi @cschetan77 - I have tidied up the commits - please can you review

@cschetan77

Copy link
Copy Markdown

Thanks @MatthewHarrigan , Could you rebase your branch as well.

…Vitest

Fixes auth0#1654.

v10.0.0 added `"type": "module"` to package.json but kept `main`
pointing at the UMD bundle (`dist/auth0.min.js`) without an `exports`
map. Bundlers that respect `"type": "module"` (Vite, Vitest, modern
esbuild) load the UMD file as ESM and find no top-level `export`
statements — so `import auth0 from 'auth0-js'` resolves to `undefined`,
breaking the documented `new auth0.WebAuth(...)` / `new auth0.Authentication(...)`
usage shown in the README.

This commit adds an `exports` map so each environment receives the
right bundle:

  - `import`  → `dist/auth0.min.esm.js` (real ESM exports)
  - `require` → `dist/auth0.min.js`     (UMD; Jest-style transformers
                                         handle the `exports.X = ...`
                                         writes correctly)
  - `default` → `dist/auth0.min.esm.js` (fallback)

`./dist/*` and `./build/*` wildcards preserve any deep-path imports
consumers may have adopted as workarounds. `./package.json` is exposed
explicitly for tools that read it.

Verified locally:

  - `npm run lint`                  clean
  - `npm run build`                 produces all 4 expected artifacts
  - `npm test`                      658 passing
  - `npm run test:es-check:es5`     pass
  - `npm run test:es-check:es2015:module`  pass
  - Resolution probe under Node 22:
      `import auth0 from 'auth0-js'`                       → object with `.Authentication`/`.WebAuth`
      `import auth0 from 'auth0-js/dist/auth0.min.esm.js'` → same (deep-path workaround preserved)
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.

v10.0.0: "type": "module" + UMD main breaks default import under Vite/Vitest (import auth0 from 'auth0-js' yields undefined)

2 participants