Skip to content

node:module: implement findPackageJSON - #37924

Closed
eduardoaugustolb wants to merge 2 commits into
oven-sh:mainfrom
eduardoaugustolb:eduardoaugustolb/fix/node-module-find-package-json
Closed

eduardoaugustolb wants to merge 2 commits into
oven-sh:mainfrom
eduardoaugustolb:eduardoaugustolb/fix/node-module-find-package-json

Conversation

@eduardoaugustolb

Copy link
Copy Markdown

Fixes #23898.

Root cause

node:module did not export or implement findPackageJSON, so a named ESM import failed before the call could run.

Change

Expose a native findPackageJSON binding and resolve through Bun’s existing ESM resolver, returning the selected package’s package.json. This preserves ESM package resolution for bare specifiers and resolved file URLs.

Validation

The added regression test runs both findPackageJSON("pkg", import.meta.url) and the issue-shaped findPackageJSON(import.meta.resolve("pkg")). It fails on the unfixed system Bun with Export named 'findPackageJSON' not found in module 'node:module'.\n\nValidated in Docker:\n\nsh\nbun bd test test/js/node/module/node-module-module.test.js\n\n\nResult: 38 passed, 1 pre-existing skip.

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Claude Code Review

This pull request is from a fork — automated review is disabled. A repository maintainer can comment @claude review to run a one-time review.

@coderabbitai

coderabbitai Bot commented Aug 12, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: 5519e2fb-6fd8-4d11-8d8a-8ae3f4f30d53

📥 Commits

Reviewing files that changed from the base of the PR and between 315136d and 39b903f.

📒 Files selected for processing (5)
  • src/jsc/NodeModuleModule.rs
  • src/jsc/VirtualMachine.rs
  • src/jsc/modules/NodeModuleModule.cpp
  • src/resolver/result.rs
  • test/js/node/module/node-module-module.test.js

Walkthrough

Changes

The node:module API now exports findPackageJSON. Bun resolves package JSON paths from package specifiers and file URLs through a new resolver mode. Integration coverage verifies both lookup forms.

findPackageJSON resolution

Layer / File(s) Summary
Package JSON resolution mode
src/resolver/result.rs, src/jsc/VirtualMachine.rs
ResolveMode::PackageJson uses package JSON paths and returns empty paths for unsupported resolver results.
Native API bridge
src/jsc/modules/NodeModuleModule.cpp, src/jsc/NodeModuleModule.rs
The host function validates arguments, converts file: URLs, delegates resolution, and returns the resolved path or undefined.
Integration validation
test/js/node/module/node-module-module.test.js
The test verifies package-specifier and resolved-URL lookups for an ESM package.

Suggested reviewers: robobun, jarred-sumner, cirospaciari

🚥 Pre-merge checks | ✅ 4
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly identifies the implementation of findPackageJSON in node:module.
Description check ✅ Passed The description explains the root cause, implementation, and validation, although it does not use the repository template headings.
Linked Issues check ✅ Passed The changes export and implement findPackageJSON, support the reported call patterns, and add regression coverage for issue #23898.
Out of Scope Changes check ✅ Passed The resolver, FFI, module export, result helper, and regression test changes all support the linked issue objectives.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@robobun

robobun commented Aug 12, 2026

Copy link
Copy Markdown
Collaborator

Thanks for this. I opened #37947, which keeps your commit as its first commit and reworks the lookup on top of it so it matches Node on the documented cases (for example findPackageJSON("..", import.meta.url), packages without a resolvable entry point, and the closest package.json for a resolved file). Details and the comparison against Node are in that PR's description.

@eduardoaugustolb eduardoaugustolb closed this by deleting the head repository Aug 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Error when importing findPackageJSON method from node:module

2 participants