Skip to content

Add package.json exports to support ES modules in Node - #131

Merged
thegecko merged 1 commit into
ARMmbed:masterfrom
reginaldalfret:feat/esm-exports-support
Nov 8, 2026
Merged

thegecko merged 1 commit into
ARMmbed:masterfrom
reginaldalfret:feat/esm-exports-support

Conversation

@reginaldalfret

Copy link
Copy Markdown
Contributor

Description

Resolves #118.

Adds an exports field to package.json to properly support ECMAScript Module (ESM) imports in modern Node.js and modern test runners (such as Vitest).

Problem

Modern Node.js ("type": "module") does not read the non-standard "module" field of package.json, falling back to "main" (dist/dap.umd.js). Because the UMD bundle is analyzed as CommonJS, Node's CJS module lexer cannot reliably detect named exports from the bundle, throwing:

SyntaxError: Named export 'CortexM' not found. The requested module 'dapjs' is a CommonJS module, which may not support all module.exports as named exports.

Solution

Added standard conditional export mappings in package.json:

  • . ->
    • types: points to ./types/index.d.ts (enabling TypeScript type resolution)
    • import: points to ./dist/dap.esm.js (enabling native named ESM imports in Node and modern bundlers)
    • require: points to ./dist/dap.umd.js (maintaining backwards compatibility for CommonJS require("dapjs"))
    • default: points to ./dist/dap.umd.js
  • ./package.json -> exposed for bundler metadata inspection.

Legacy fields (main, module, types) remain intact for older build tooling.

Verification

  1. Built project via npm run build cleanly (dist/dap.umd.js, dist/dap.esm.js, and TypeScript types).
  2. Reproduced the original failure in an ESM Node project ("type": "module") with import { CortexM } from "dapjs".
  3. Verified import { CortexM } from "dapjs" resolves and loads CortexM function cleanly with the exports mapping.
  4. Verified CommonJS const { CortexM } = require("dapjs") resolves and loads CortexM function cleanly.

Fixes #118

Resolve ARMmbed#118 by adding an `exports` map to `package.json`.

Modern Node.js environments and test runners (e.g. Vitest) using ES modules
do not read the legacy "module" field and fall back to "main" (the UMD bundle),
failing named imports (`SyntaxError: Named export '...' not found`).

Adding standard conditional exports maps:
- `types` -> `./types/index.d.ts` (for TypeScript resolution)
- `import` -> `./dist/dap.esm.js` (for ESM imports in Node/bundlers)
- `require` -> `./dist/dap.umd.js` (for CommonJS require)
- `default` -> `./dist/dap.umd.js`
- `./package.json` -> `./package.json`

Existing legacy fields (`main`, `module`, `types`) remain intact for backwards
compatibility with older tooling.

Fixes ARMmbed#118

Signed-off-by: Reginald Alfret <reginaldalfret@gmail.com>

@thegecko thegecko left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@thegecko
thegecko merged commit 7e48307 into ARMmbed:master Nov 8, 2026
1 check passed
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.

Add package.json exports to support ES modules in Node

2 participants