Title: Node throws error when require'ing ESM module in TypeScript in nested ESM/CJS context · Issue #59963 · nodejs/node · GitHub
Open Graph Title: Node throws error when require'ing ESM module in TypeScript in nested ESM/CJS context · Issue #59963 · nodejs/node
X Title: Node throws error when require'ing ESM module in TypeScript in nested ESM/CJS context · Issue #59963 · nodejs/node
Description: Version v24.8.0 Platform Darwin mac.home 24.5.0 Darwin Kernel Version 24.5.0: Tue Apr 22 19:53:27 PDT 2025; root:xnu-11417.121.6~2/RELEASE_ARM64_T6041 arm64 Subsystem No response What steps will reproduce the bug? Reproducing this requir...
Open Graph Description: Version v24.8.0 Platform Darwin mac.home 24.5.0 Darwin Kernel Version 24.5.0: Tue Apr 22 19:53:27 PDT 2025; root:xnu-11417.121.6~2/RELEASE_ARM64_T6041 arm64 Subsystem No response What steps will re...
X Description: Version v24.8.0 Platform Darwin mac.home 24.5.0 Darwin Kernel Version 24.5.0: Tue Apr 22 19:53:27 PDT 2025; root:xnu-11417.121.6~2/RELEASE_ARM64_T6041 arm64 Subsystem No response What steps will re...
Opengraph URL: https://github.com/nodejs/node/issues/59963
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Node throws error when require'ing ESM module in TypeScript in nested ESM/CJS context","articleBody":"### Version\n\nv24.8.0\n\n### Platform\n\n```text\nDarwin mac.home 24.5.0 Darwin Kernel Version 24.5.0: Tue Apr 22 19:53:27 PDT 2025; root:xnu-11417.121.6~2/RELEASE_ARM64_T6041 arm64\n```\n\n### Subsystem\n\n_No response_\n\n### What steps will reproduce the bug?\n\nReproducing this requires 2 `.mts` and 1 `.cts` file:\n\n`file1.mts`\n```\nimport { message } from \"./file2.cts\";\n\nconsole.log(message);\n```\n\n`file2.cts`\n```\nconst { message } = require(\"./file3.mts\");\n\nmodule.exports = { message };\n```\n\n`file3.mts`\n```\nexport const message = \"Hello from file3.mts\";\n```\n\n\nA similar error is reproducable with 2 `.cts` files and 1 `.mts` file:\n\n`file1.cts`\n```\nconst { message } = require(\"./file2.mts\");\n\nconsole.log(message);\n```\n\n`file2.mts`\n```\nexport { message } from \"./file3.cts\";\n```\n\n`file3.cts`\n```\nconst message = \"Hello from file3.cts\";\n\nmodule.exports = { message };\n```\n\nRunning `node file1.mts` (or `node file1.cts`) triggers the error.\n\n### How often does it reproduce? Is there a required condition?\n\nAlways, requires a node.js version that supports require'ing ESM module and running TS by stripping types\n\n### What is the expected behavior? Why is that the expected behavior?\n\nI'd expect node to allow me to require ESM modules even in these \"nested\" cases. Exactly the same code works fine in pure JavaScript, this only errors when importing/requiring TypeScript files directly.\n\n### What do you see instead?\n\n```\nnode:internal/assert:11\n throw new ERR_INTERNAL_ASSERTION(message);\n ^\n\nError [ERR_INTERNAL_ASSERTION]: Imported CJS module file:///Users/sjoerdvanbommel/workspace/personal/lab/esm-cjs-esm/file2.cts failed to load module file:///Users/sjoerdvanbommel/workspace/personal/lab/esm-cjs-esm/file3.mts using require()\nThis is caused by either a bug in Node.js or incorrect usage of Node.js internals.\nPlease open an issue with this stack trace at https://github.com/nodejs/node/issues\n\n at assert (node:internal/assert:11:11)\n at require (node:internal/modules/esm/translators:158:5)\n at Object.\u003canonymous\u003e (file:///Users/sjoerdvanbommel/workspace/personal/lab/esm-cjs-esm/file2.cts:1:21)\n at loadCJSModule (node:internal/modules/esm/translators:178:3)\n at ModuleWrap.\u003canonymous\u003e (node:internal/modules/esm/translators:221:7)\n at ModuleJob.run (node:internal/modules/esm/module_job:371:25)\n at async onImport.tracePromise.__proto__ (node:internal/modules/esm/loader:702:26)\n at async asyncRunEntryPointWithESMLoader (node:internal/modules/run_main:101:5) {\n code: 'ERR_INTERNAL_ASSERTION'\n}\n\nNode.js v24.8.0\n```\n\n### Additional information\n\nThis error only occurs in cases like ESM -\u003e CJS -\u003e ESM or CJS -\u003e ESM -\u003e CJS and when using TypeScript files directly. When there's only a single import/require from ESM -\u003e CJS or CJS -\u003e ESM, or when using JavaScript, it works as expected.","author":{"url":"https://github.com/sjoerdvanBommel","@type":"Person","name":"sjoerdvanBommel"},"datePublished":"2025-09-21T17:12:13.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":1},"url":"https://github.com/59963/node/issues/59963"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:e20cfd74-c842-8304-452b-284ab00696b2 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | DF1E:32FDD1:3EE35:56590:6A4C6D95 |
| html-safe-nonce | 7a72d5a3940af701879ae524f0201d6923f8185282c199c75e159a8770e685c7 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJERjFFOjMyRkREMTozRUUzNTo1NjU5MDo2QTRDNkQ5NSIsInZpc2l0b3JfaWQiOiI4MTU0MTE4NzEzMTYwNTI3MjUzIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | b80e00b3e9bbde7c34062fa65e9ebb756cfe3460243d213affa9639194c2317d |
| hovercard-subject-tag | issue:3438423842 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/nodejs/node/59963/issue_layout |
| twitter:image | https://opengraph.githubassets.com/e691dbf9efdec8ab36f9c8a67c90964415247a2040dc4364c85e01a4b30220df/nodejs/node/issues/59963 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/e691dbf9efdec8ab36f9c8a67c90964415247a2040dc4364c85e01a4b30220df/nodejs/node/issues/59963 |
| og:image:alt | Version v24.8.0 Platform Darwin mac.home 24.5.0 Darwin Kernel Version 24.5.0: Tue Apr 22 19:53:27 PDT 2025; root:xnu-11417.121.6~2/RELEASE_ARM64_T6041 arm64 Subsystem No response What steps will re... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | sjoerdvanBommel |
| hostname | github.com |
| expected-hostname | github.com |
| None | 3d11bb817438277de2a940854450e83a7d32b6aeb5014e9e6b00a6423900251c |
| turbo-cache-control | no-preview |
| go-import | github.com/nodejs/node git https://github.com/nodejs/node.git |
| octolytics-dimension-user_id | 9950313 |
| octolytics-dimension-user_login | nodejs |
| octolytics-dimension-repository_id | 27193779 |
| octolytics-dimension-repository_nwo | nodejs/node |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 27193779 |
| octolytics-dimension-repository_network_root_nwo | nodejs/node |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 14099438da5379150f15a2892474c7c7e6c0e55e |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width