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

Relative require() in commonjs source not processed by ESM Loader #53198

Open
privatenumber opened this issue May 29, 2024 · 2 comments
Open
Labels
confirmed-bug Issues with confirmed bugs. loaders Issues and PRs related to ES module loaders

Comments

@privatenumber
Copy link
Contributor

Version

v22.2.0

Platform

Darwin mac.local 23.4.0 Darwin Kernel Version 23.4.0: Fri Mar 15 00:12:49 PDT 2024; root:xnu-10063.101.17~1/RELEASE_ARM64_T6020 arm64

Subsystem

No response

What steps will reproduce the bug?

'use strict';

require('node:module').register(
  'data:text/javascript,' + `
  import assert from 'node:assert';
  
  export ${encodeURIComponent(
    function resolve(specifier, context, nextResolve) {
      console.log({ specifier, context });
      return nextResolve(specifier, context);
    },
  )}
  
  export ${encodeURIComponent(
    function load(url, context, nextLoad) {
      if (url === 'custom:cjs') {
        return {
          format: 'commonjs',
          source: 'console.log(require("./relative"))',
          shortCircuit: true,
        };
      }
      return nextLoad(url, context);
    }
  )}
  `,
);

import('custom:cjs').then(console.log, console.error);

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

Consistently

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

The load hook doc says:

When a source is provided, all require calls from this module will be processed by the ESM loader with registered resolve and load hooks

https://nodejs.org/api/module.html#resolvespecifier-context-nextresolve:~:text=When%20a%20source,will%20not%20apply.

What do you see instead?

require() calls with relative path are still processed by the CommonJS loader

Error: Cannot find module './relative'
Require stack:
- custom:cjs
    at Module._resolveFilename (node:internal/modules/cjs/loader:1186:15)
    at require (node:internal/modules/esm/translators:195:30)
    at Object.<anonymous> (custom:cjs:1:13)
    at loadCJSModule (node:internal/modules/esm/translators:223:3)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/translators:258:7)
    at ModuleJob.run (node:internal/modules/esm/module_job:262:25)
    at async ModuleLoader.import (node:internal/modules/esm/loader:475:24) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [ 'custom:cjs' ]
}

Additional information

No response

@aduh95
Copy link
Contributor

aduh95 commented May 29, 2024

require() calls with relative path are still processed by the CommonJS loader

That's not correct, they are processed by your custom loader. If you tweak your resolve hook to not call the next function when it receives ./relative, you won't see the error. The reason you won't see the console.log output every time is documented here:

node/doc/api/module.md

Lines 391 to 394 in fac55e3

Hooks are run in a separate thread, isolated from the main thread where
application code runs. That means it is a different [realm][]. The hooks thread
may be terminated by the main thread at any time, so do not depend on
asynchronous operations (like `console.log`) to complete.

AFAICT, working as expected, so closing. Let me know if I missed something.

@aduh95 aduh95 closed this as not planned Won't fix, can't repro, duplicate, stale May 29, 2024
@aduh95
Copy link
Contributor

aduh95 commented May 29, 2024

Actually, you were correct, the stack trace shows MODULE_NOT_FOUND, not ERR_MODULE_NOT_FOUND, which is an indication that the error is indeed returned by the CJS loader, sorry for answering too quickly.

/cc @nodejs/loaders

@aduh95 aduh95 reopened this May 29, 2024
@VoltrexKeyva VoltrexKeyva added the loaders Issues and PRs related to ES module loaders label May 29, 2024
@mcollina mcollina added the confirmed-bug Issues with confirmed bugs. label Jun 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed-bug Issues with confirmed bugs. loaders Issues and PRs related to ES module loaders
Projects
None yet
Development

No branches or pull requests

4 participants