Skip to content

registerHooks: TypeError when json file is required in hook and in the imported file #57358

@timokoessler

Description

@timokoessler

Version

v23.9.0

Platform

Darwin Mac 24.3.0 Darwin Kernel Version 24.3.0: Thu Jan  2 20:24:23 PST 2025; root:xnu-11215.81.4~3/RELEASE_ARM64_T8122 arm64

Subsystem

No response

What steps will reproduce the bug?

Create the following files:

instrument.js

const mod = require("module");

mod.registerHooks({
  load(url, context, nextLoad) {
    const result = nextLoad(url, context);
    if (context.format === "json") {
      // We don't want to modify the JSON file
      return result;
    }

    // Normally we would extract the path to the package from the url and read the root package.json
    console.log(
      `Patching package with version ${require("./package.json").version}`
    );

    return result;
  },
});

app

console.log("Hello from app.js");
console.log(`My version is "${require("./package.json").version}"`);

package.json

{
  "version": "1.0.0",
  "type": "commonjs"
}

Run node --import ./instrument.js ./app.js

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

Always, as long as the same json file is imported using require.

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

Script does not throw an exception and outputs:

Patching package with version 1.0.0
Hello from app.js
My version is "1.0.0"

What do you see instead?

node --import ./instrument.js ./app.js
Patching package with version 1.0.0
Hello from app.js
node:internal/modules/esm/translators:151
    return cjsCache.get(job.url).exports;
                                ^

TypeError: Cannot read properties of undefined (reading 'exports')
    at require (node:internal/modules/esm/translators:151:33)
    at Object.<anonymous> (/Users/timokoessler/Git/nodejs-module-hooks-bug/package-json/app.js:2:31)
    at loadCJSModule (node:internal/modules/esm/translators:165:3)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/translators:204:7)
    at ModuleJob.run (node:internal/modules/esm/module_job:273:25)
    at async onImport.tracePromise.__proto__ (node:internal/modules/esm/loader:600:26)
    at async asyncRunEntryPointWithESMLoader (node:internal/modules/run_main:98:5)

Additional information

The package.json file is required during the hook to check if we support the installed package version, but the issue is not limited to package.json files and happens with all json files.

Workaround: Use JSON.parse(readFileSync(...))

Metadata

Metadata

Assignees

No one assigned

    Labels

    loadersIssues and PRs related to ES module loaders

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions