Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
timokoessler opened this issue Mar 7, 2025 · 1 comment
Labels
loaders Issues and PRs related to ES module loaders

Comments

@timokoessler
Copy link

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(...))

@joyeecheung
Copy link
Member

If you run it with node --require ./instrument.js ./app.js it would run as normal. --import uses the ESM loader which re-invents the require() function in imported CJS files that triggers the hooks in a way that's not working quite naturally with the CJS loader.

In essence I think the fix would still be similar to #57327 (comment) - revert the approach in #47999 and do not re-invent the require() function in the ESM loader. That has been causing several other issues as well.

@joyeecheung joyeecheung added the loaders Issues and PRs related to ES module loaders label Mar 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
loaders Issues and PRs related to ES module loaders
Projects
None yet
Development

No branches or pull requests

2 participants