nodejs / nodejs/node

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

Open
#53,198 2 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

confirmed-bug loaders
Dominant language
JavaScript
Stars
122k
Forks
37.3k
Avg merge
4d 2h
Merged PRs (30d)
283

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the provided reproduction on Node.js v22.2.0 and trace the ESM loader and CommonJS translator frames shown in the stack. Done means a relative require from hook-provided CommonJS source is processed through the registered resolve and load hooks, as stated in the load-hook documentation.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, node.js
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.