Skip to content

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

@microbit-matt-hillsdon

Description

@microbit-matt-hillsdon

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions