Repository navigation
Add package.json exports to support ES modules in Node - #131
Merged
Merged
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Resolves #118.
Adds an
exportsfield topackage.jsonto 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 ofpackage.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: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 CommonJSrequire("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
npm run buildcleanly (dist/dap.umd.js,dist/dap.esm.js, and TypeScript types)."type": "module") withimport { CortexM } from "dapjs".import { CortexM } from "dapjs"resolves and loadsCortexMfunction cleanly with theexportsmapping.const { CortexM } = require("dapjs")resolves and loadsCortexMfunction cleanly.Fixes #118