Add package.json exports to support ES modules in Node
- Dominant language
- TypeScript
- Stars
- 134
- Forks
- 56
- PR merge metrics
- No merged PRs in 30d
Description
It would be great to update package.json to use [exports](https://nodejs.org/api/packages.html#exports).
Modern node understands ES modules but doesn't understand the module field of package.json (as used by bundlers etc) so gives an error on import.
We hit this using tests written with [Vitest](https://vitest.dev/) but it reproduces in trivial Node project with `"type": "module"` set in package.json and any named `import` from dapjs.
The fix that Node suggests does work, but unfortunately I can't make TypeScript happy with that approach in my real project.
```bash
➜ daptest node index.js
file:///Users/mth/Development/microbit/daptest/index.js:1
import { CortexM } from "dapjs";
^^^^^^^
SyntaxError: Named export 'CortexM' not found. The requested module 'dapjs' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:
import pkg from 'dapjs';
const { CortexM } = pkg;
at ModuleJob._instantiate (node:internal/modules/esm/module_job:180:21)
at async ModuleJob.run (node:internal/modules/esm/module_job:263:5)
at async onImport.tracePromise.__proto__ (node:internal/modules/esm/loader:547:26)
at async asyncRunEntryPointWithESMLoader (node:internal/modules/run_main:116:5)
Node.js v22.13.1
```
Contributor guide
No contributing guide indexed for this repository
Research direction
Start with package.json and reproduce the failure in a trivial Node project using type: module, a named import from dapjs, or the reported Vitest setup. Check the Node exports documentation and the existing package configuration; done means named imports work in modern Node ES modules without breaking TypeScript compatibility.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- node.js, typescript
- Domain
- tooling
- Issue type
- Feature
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100