Skip to content

Node 22.14 added findPackageJSON #3584

Closed

Description

@tido64

Investigate whether we can use module.findPackageJSON instead of our custom resolver (when detected).

Activity

  1. tido64 commented on May 12, 2026

    @tido64
    MemberAuthor

    Based on my experiments, we will not be using findPackageJSON in its current state. It's too particular about the input and the documentation does not reflect the actual implementation. Examples:

    ❌ Find package.json based on package specifier alone:

    > findPackageJSON("react-native")
    Uncaught TypeError: Invalid URL
        at getPackageScopeConfig (node:internal/modules/package_json_reader:208:33)
        at getPackageJSONURL (node:internal/modules/package_json_reader:284:25)
        at Function.findPackageJSON (node:internal/modules/package_json_reader:341:33) {
      code: 'ERR_INVALID_URL',
      input: './package.json',
      base: 'data:'
    }
    

    ❌ Find package.json based on package specifier and base directory:

    > findPackageJSON("react-native", process.cwd())
    Uncaught:
    Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'react-native' imported from /~/packages/test-app
        at getPackageJSONURL (node:internal/modules/package_json_reader:314:9)
        at Function.findPackageJSON (node:internal/modules/package_json_reader:341:33) {
      code: 'ERR_MODULE_NOT_FOUND'
    }
    

    ✅ Find package.json based on package specifier and file in base directory:

    > findPackageJSON("react-native", process.cwd() + "/package.json")
    '/~/packages/test-app/node_modules/react-native/package.json'
    

    ❌ Find package.json based on absolute path to package:

    > findPackageJSON(require.resolve("react-native"))
    Uncaught:
    TypeError [ERR_UNSUPPORTED_RESOLVE_REQUEST]: Failed to resolve module specifier "/~/node_modules/.store/react-native-virtual-4431a9491c/package/index.js" from "data:": Invalid relative URL or base scheme is not hierarchical.
        at moduleResolve (node:internal/modules/esm/resolve:839:21)
        at defaultResolve (node:internal/modules/esm/resolve:985:11)
        at Function.findPackageJSON (node:internal/modules/package_json_reader:354:22) {
      code: 'ERR_UNSUPPORTED_RESOLVE_REQUEST',
      cause: TypeError: Invalid URL
          at new URL (node:internal/url:818:25)
          at moduleResolve (node:internal/modules/esm/resolve:837:18)
          at defaultResolve (node:internal/modules/esm/resolve:985:11)
          at Function.findPackageJSON (node:internal/modules/package_json_reader:354:22)
          at REPL35:1:24
          at ContextifyScript.runInThisContext (node:vm:137:12)
          at REPLServer.defaultEval (node:repl:593:22)
          at bound (node:domain:433:15)
          at REPLServer.runBound [as eval] (node:domain:444:12)
          at REPLServer.onLine (node:repl:926:10) {
        code: 'ERR_INVALID_URL',
        input: '/~/node_modules/.store/react-native-virtual-4431a9491c/package/index.js',
        base: 'data:'
      }
    }
    

    ❌ Find package.json based on absolute path to package and empty base:

    > findPackageJSON(require.resolve("react-native"), "")
    Uncaught TypeError: Invalid URL
        at new URL (node:internal/url:818:25)
        at Function.findPackageJSON (node:internal/modules/package_json_reader:336:63) {
      code: 'ERR_INVALID_URL',
      input: ''
    }
    

    ✅ Find package.json based on absolute path to package and base directory:

    > findPackageJSON(require.resolve("react-native"), process.cwd())
    '/~/node_modules/.store/react-native-virtual-4431a9491c/package/package.json'
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions