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

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

Open
microbit-matt-hillsdon opened this issue Jan 27, 2025 · 1 comment
Open

Comments

@microbit-matt-hillsdon
Copy link

It would be great to update package.json to use 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 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.

➜  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
@thegecko
Copy link
Member

PRs welcome ;)

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

No branches or pull requests

2 participants