ESM build cannot be imported in Node.js: `dist/` emits extensionless relative specifiers

Open Beginner friendly
#4 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
72/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
node.js, typescript
Domain
build-system

Research direction

The ESM output is produced by tsconfig.build.json, which sets module to ESNext and moduleResolution to bundler, so relative imports like ./api stay extensionless in dist/*.js. Start by reading that config and the build script in package.json, then reproduce by running the build and importing dist/index.js with node. Done means the ESM dist loads under Node without ERR_MODULE_NOT_FOUND, and the CJS output still works.

Written by the indexing model from the issue text.

Description

Importing @craftycodie/cstruct in a Node.js ES module fails at load time with ERR_MODULE_NOT_FOUND, whereas require("@craftycodie/cstruct") works via dist-cjs/index.js.

npm init -y && npm install @craftycodie/[email protected]
node --input-type=module -e 'import { c } from "@craftycodie/cstruct"; console.log(c.sizeof(c.struct({ x: c.u8() })));'

Actual output:

Error [ERR_MODULE_NOT_FOUND]: Cannot find module '.../node_modules/@craftycodie/cstruct/dist/api' imported from .../node_modules/@craftycodie/cstruct/dist/index.js

Expected output:

1

tsconfig.build.json compiles the ESM output with "module": "ESNext" and "moduleResolution": "bundler", so tsc preserves extensionless relative and directory specifiers (such as "./api" and "./field-types") in dist/*.js, which Node's ESM resolver rejects.

Versions tested: @craftycodie/[email protected] and main (9421e72), Node.js v26.13.0.

Credit: https://ucsbplse.github.io/

Dominant language
TypeScript
Stars
4
Forks
0
Avg merge
21d 8h
Merged PRs (30d)
1

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.