Skip to content

require() in an imported CommonJS module returns empty exports for a re-exported dependency after require.cache deletion #67611

Open

Description

@dcastil

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 Darwin Kernel Version 27.0.0: Tue Aug 11 21:05:48 PDT 2026; root:xnu-13432.1.9~1/RELEASE_ARM64_T6041 arm64

Subsystem

module

What steps will reproduce the bug?

Save as repro.mjs and run node repro.mjs:

import { mkdtempSync, realpathSync, writeFileSync } from 'node:fs'
import { createRequire } from 'node:module'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { pathToFileURL } from 'node:url'

const require = createRequire(import.meta.url)
const dir = realpathSync(mkdtempSync(join(tmpdir(), 'repro-')))
const entry = join(dir, 'entry.cjs')
const dep = join(dir, 'dep.cjs')

// Works as expected when changed to: module.exports = { nested: { dep: require('./dep.cjs') } }
writeFileSync(entry, "module.exports = require('./dep.cjs')\n")

for (const value of [1, 2, 3]) {
    writeFileSync(dep, `module.exports = { value: ${value} }\n`)
    delete require.cache[entry]
    delete require.cache[dep]
    const { default: exports } = await import(`${pathToFileURL(entry)}?v=${value}`)
    console.log(value, exports)
}

How often does it reproduce? Is there a required condition?

Always, from the second import() on. Conditions:

  • The CommonJS file is loaded with import(), not require(). Replacing the import with require(entry) prints the expected values.
  • 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.
  • 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.

What is the expected behavior? Why is that the expected behavior?

1 { value: 1 }
2 { value: 2 }
3 { value: 3 }

Both 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.

What do you see instead?

1 { value: 1 }
2 {}
3 {}

If dep.cjs contains a syntax error at the second import, the import also resolves with {} and no error is thrown.

Additional information

From reading lib/internal/modules/cjs/loader.js and lib/internal/modules/esm/translators.js, this seems to be the cause:

  1. cjsPreparseModuleExports() adds an unloaded Module._cache entry (marked kIsCachedByESMLoader) for each re-exported dependency before the imported file runs.
  2. 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.
  3. 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.

Two 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.

Applying 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.

Found 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.

Also reproduces on v24.22.3, v22.23.5 and v22.18.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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