nodejs / nodejs/node

Hooks loading `commonjs` files uses `import` condition for `require()`

Open
#51,327 9 comments 7 reactions 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

21.5.0

Platform

macOS

Subsystem

No response

What steps will reproduce the bug?

Reproduction: https://github.com/privatenumber/bug-node-hooks-cjs-conditions

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

A Hook needs to be registered to load commonjs type files.

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

For the conditions to include require when require(file) is executed.

What do you see instead?

The import conditions only include import.

Here are the logs:

resolve: {
	"url": "file:///repro/src/index.cjs",
	"context": {
		"conditions": [
			"node",
			"import",
			"node-addons"
		],
		"importAttributes": {}
	}
}
load: {
	"url": "file:///repro/src/index.cjs",
	"context": {
		"format": "commonjs",
		"importAttributes": {}
	}
}
resolve: {
	"url": "file:///repro/src/file.cjs",
	"context": {
		"conditions": [
			"node",
			"import",
			"node-addons"
		],
		"importAttributes": {},
		"parentURL": "file:///repro/src/index.cjs"
	}
}
load: {
	"url": "file:///repro/src/file.cjs",
	"context": {
		"format": "commonjs",
		"importAttributes": {}
	}
}

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 with the linked reproduction and inspect the hook resolve and load contexts for CommonJS files. Compare the reported conditions during require(file) with the expected require condition; done means the hook receives the correct condition while loading CommonJS files and the reproduction no longer shows only import.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.