Title: `require()` in an imported CommonJS module returns empty exports for a re-exported dependency after `require.cache` deletion · Issue #67611 · nodejs/node · GitHub
Open Graph Title: `require()` in an imported CommonJS module returns empty exports for a re-exported dependency after `require.cache` deletion · Issue #67611 · nodejs/node
X Title: `require()` in an imported CommonJS module returns empty exports for a re-exported dependency after `require.cache` deletion · Issue #67611 · nodejs/node
Description: Disclosure: AI helped narrow this down and draft the report. I've read everything, ran the repro and checked the source references. I can open a PR with a test if this is considered a bug. Version 26.13.1 Platform Darwin Mac 27.0.0 Darwi...
Open Graph Description: Disclosure: AI helped narrow this down and draft the report. I've read everything, ran the repro and checked the source references. I can open a PR with a test if this is considered a bug. Version ...
X Description: Disclosure: AI helped narrow this down and draft the report. I've read everything, ran the repro and checked the source references. I can open a PR with a test if this is considered a bug. Vers...
Opengraph URL: https://github.com/nodejs/node/issues/67611
X: @github
Domain: github.com
IPv4 address: 140.82.114.3
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"`require()` in an imported CommonJS module returns empty exports for a re-exported dependency after `require.cache` deletion","articleBody":"> Disclosure: AI helped narrow this down and draft the report. I've read everything, ran the repro and checked the source references.\n\nI can open a PR with a test if this is considered a bug.\n\n### Version\n\n26.13.1\n\n### Platform\n\n```text\nDarwin Mac 27.0.0 Darwin Kernel Version 27.0.0: Tue Aug 11 21:05:48 PDT 2026; root:xnu-13432.1.9~1/RELEASE_ARM64_T6041 arm64\n```\n\n### Subsystem\n\n`module`\n\n### What steps will reproduce the bug?\n\nSave as `repro.mjs` and run `node repro.mjs`:\n\n```js\nimport { mkdtempSync, realpathSync, writeFileSync } from 'node:fs'\nimport { createRequire } from 'node:module'\nimport { tmpdir } from 'node:os'\nimport { join } from 'node:path'\nimport { pathToFileURL } from 'node:url'\n\nconst require = createRequire(import.meta.url)\nconst dir = realpathSync(mkdtempSync(join(tmpdir(), 'repro-')))\nconst entry = join(dir, 'entry.cjs')\nconst dep = join(dir, 'dep.cjs')\n\n// Works as expected when changed to: module.exports = { nested: { dep: require('./dep.cjs') } }\nwriteFileSync(entry, \"module.exports = require('./dep.cjs')\\n\")\n\nfor (const value of [1, 2, 3]) {\n writeFileSync(dep, `module.exports = { value: ${value} }\\n`)\n delete require.cache[entry]\n delete require.cache[dep]\n const { default: exports } = await import(`${pathToFileURL(entry)}?v=${value}`)\n console.log(value, exports)\n}\n```\n\n### How often does it reproduce? Is there a required condition?\n\nAlways, from the second `import()` on. Conditions:\n\n- The CommonJS file is loaded with `import()`, not `require()`. Replacing the import with `require(entry)` prints the expected values.\n- Its export has a shape the CommonJS lexer reports as a re-export: `module.exports = require('./dep.cjs')`, `module.exports = { ...require('./dep.cjs') }`, or `module.exports = { key: require('./dep.cjs') }`. With the `require` nested one level deeper, or assigned to a variable first, it works.\n- Both files were deleted from `require.cache` and the file is imported again under a new URL. This is the usual way tools reload a config file and its dependencies.\n\n### What is the expected behavior? Why is that the expected behavior?\n\n```\n1 { value: 1 }\n2 { value: 2 }\n3 { value: 3 }\n```\n\nBoth modules were removed from `require.cache`, so both should be evaluated again, as they are when the entry is loaded with `require()` or when the `require` call is not in a re-export position. There is no circular dependency between the two files.\n\n### What do you see instead?\n\n```\n1 { value: 1 }\n2 {}\n3 {}\n```\n\nIf `dep.cjs` contains a syntax error at the second import, the import also resolves with `{}` and no error is thrown.\n\n### Additional information\n\nFrom reading `lib/internal/modules/cjs/loader.js` and `lib/internal/modules/esm/translators.js`, this seems to be the cause:\n\n1. `cjsPreparseModuleExports()` adds an unloaded `Module._cache` entry (marked `kIsCachedByESMLoader`) for each re-exported dependency before the imported file runs.\n2. On the first import, the file's `require('./dep.cjs')` goes through the regular cache check in `Module._load()`, which recognizes such an entry, marks it `kModuleCircularVisited` and loads it.\n3. That load also records the request in `relativeResolveCache`. On later imports the fast path at the top of `Module._load()` finds the new, still unloaded entry and returns `getExportsForCircularRequire(cachedModule)` for any `!cachedModule.loaded`, without the `kIsCachedByESMLoader` / `kModuleCircularVisited` check the regular path has.\n\nTwo observations support this: logging `require.cache[dep]` from inside `entry.cjs` right before its `require()` shows the same unloaded entry with `kIsCachedByESMLoader` set on every import, including the first one that works. And giving the entry a different but equivalent request string on each import (`./x/../dep.cjs`, `./x/../x/../dep.cjs`, …), so the fast path cannot hit, makes every import return the correct exports.\n\nApplying the same check in the fast path, and falling through to the regular path for an ESM-loader entry that has not been visited, looks like it would fix it.\n\nFound through a dev server that reloads Tailwind CSS config files this way: a config written as `module.exports = require('./shared.cjs')` lost its values on the second edit. Possibly relevant to #61767.\n\nAlso reproduces on v24.22.3, v22.23.5 and v22.18.0.","author":{"url":"https://github.com/dcastil","@type":"Person","name":"dcastil"},"datePublished":"2026-11-19T12:23:50Z","url":"https://github.com/nodejs/node/issues/67611"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| 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:4d7a61f3-5310-de08-5330-7e40b8a2d56b |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | DEB8:1D768B:4FA5EE:681580:6B061EFA |
| html-safe-nonce | 7a0a60ca9061e18b384cd6b7926a3cea55bf4a9e4e9c8d2766e7fe5cd0090114 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJERUI4OjFENzY4Qjo0RkE1RUU6NjgxNTgwOjZCMDYxRUZBIiwidmlzaXRvcl9pZCI6IjgyNDY3MDUyMDI5MTM0ODQ1MzgiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | bd6dfa39c794844be0c3702dd0472d1941b7f19c2e884da17515f6b6094bef36 |
| hovercard-subject-tag | issue:6489502469 |
| 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/67611/issue_layout |
| twitter:image | https://opengraph.githubassets.com/662fd66a11cdba29293e0e1f5a5bb2e31add08764e42b68810b536d0cf71359e/nodejs/node/issues/67611 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/662fd66a11cdba29293e0e1f5a5bb2e31add08764e42b68810b536d0cf71359e/nodejs/node/issues/67611 |
| og:image:alt | Disclosure: AI helped narrow this down and draft the report. I've read everything, ran the repro and checked the source references. I can open a PR with a test if this is considered a bug. Version ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | dcastil |
| hostname | github.com |
| expected-hostname | github.com |
| None | 9a7c5824b75ea2ec5e3de73a483229c7bb9f32a774ffd401f010dd9bacf1483d |
| 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 | b25c3080e84402f75431d8dc7a96b2635219cec7 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
| Server | github.com |
| Content-Type | text/html; charset=utf-8 |
| Cache-Control | max-age=0, private, must-revalidate |
Viewport: width=device-width