Skip to content

Fix the package entry point failing to load under Node ESM (ERR_MODULE_NOT_FOUND) - #153

Open
eastagiletracker wants to merge 1 commit into
gonativeio:mainfrom
eastagiletracker:agile-board/fix-esm-import-extension
Open

Fix the package entry point failing to load under Node ESM (ERR_MODULE_NOT_FOUND)#153
eastagiletracker wants to merge 1 commit into
gonativeio:mainfrom
eastagiletracker:agile-board/fix-esm-import-extension

Conversation

@eastagiletracker

Copy link
Copy Markdown

This PR proposes fixing the package entry point, which fails to load for npm consumers under Node's ESM resolver with ERR_MODULE_NOT_FOUND. We include this PR work along with a full history of your repo at https://eastagiletracker.com/projects/339. You can sign in with your GitHub ID to claim ownership of the project.

What happens today

src/plugins/shareIntoApp.ts imports '../utils/listener' with no file extension. tsc emits that specifier verbatim, so dist/plugins/shareIntoApp.js ships it, and Node's ESM resolver does no extension guessing — resolution of the entire entry graph fails, not just that one module. Every other module in src/ already uses an explicit .js specifier, which is the invariant "Fix ESM import issue" (#125) established when it closed the ERR_UNSUPPORTED_DIR_IMPORT report in #123; "Move plugin listeners to the plugin files" (#131) landed one specifier without it afterwards.

It reproduces against the published package, so no checkout is needed — in an empty directory whose package.json has "type": "module":

npm pack median-js-bridge@2.20.0
npm i ./median-js-bridge-2.20.0.tgz
node -e 'import("median-js-bridge")'
Error [ERR_MODULE_NOT_FOUND]: Cannot find module '<cwd>/node_modules/median-js-bridge/dist/utils/listener'
imported from '<cwd>/node_modules/median-js-bridge/dist/plugins/shareIntoApp.js'

The same failure comes from a fresh build of main at b4bcd62 (npm ci && npm run build, then importing ./dist/index.js). Bundlers that guess extensions (a plain Vite or webpack browser build) resolve it and are unaffected; anything going through Node's own resolver — SSR builds, test runners, plain node scripts — cannot load the library at all.

The change

  • src/plugins/shareIntoApp.ts: '../utils/listener''../utils/listener.js'. This is the failing specifier.
  • src/plugins/msdynamics.ts: '../commands/general''../commands/general.js'. This one is type-only, so tsc erases it and it does not fail today; corrected so it cannot turn into a runtime import later.
  • test/bundle.test.js: a check that imports the built dist/index.js through Node's real ESM loader, so npm test covers the entry point that regressed. The runner now awaits check(), which leaves the existing sixteen checks untouched.

Verification

On main at b4bcd62, npm run lint, npm run build and npm test are all green (16 passed, 0 failed) — that is the baseline this is measured against. With the new check added but the source fix reverted, it is the only failure, and it fails with exactly the error above:

❌ dist/index.js resolves through the Node ESM loader (Error: Cannot find module '.../dist/utils/listener' imported from '.../dist/plugins/shareIntoApp.js')

📊 Results: 16 passed, 1 failed

With the fix applied: lint clean, build clean, 📊 Results: 17 passed, 0 failed — no new failures against the baseline. As an end-to-end check, npm pack on this branch and installing that tarball into the same "type": "module" directory imports cleanly, where the published 2.20.0 tarball raises ERR_MODULE_NOT_FOUND.

How this was managed

We imported this repository's issues and pull requests onto a live board and tracked the work there: this change is the story Package entry point fails to load under Node ESM resolution, on the board at https://eastagiletracker.com/projects/339.

board

If you'd rather not receive contributions like this, reply no-more-prs on this pull request and we won't open any further ones on your repositories.


Lawrence W. Sinclair
CEO / East Agile
linkedin.com/in/lwsinclair/
eastagile.com

src/plugins/shareIntoApp.ts imported '../utils/listener' without a file
extension, so dist/plugins/shareIntoApp.js carries the same specifier and
Node's ESM resolver, which does no extension guessing, fails the whole
entry graph with ERR_MODULE_NOT_FOUND. Every other module already uses an
explicit .js specifier. Restore it here, and on the type-only import in
msdynamics.ts that is erased at build time today.

Add a test that imports the built dist/index.js through the real ESM
loader, which is the check that was missing when this regressed.
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

Successfully merging this pull request may close these issues.

1 participant