Skip to content

Could not resolve "#node-web-compat" - subpath imports with esbuild and yarn pnp #2545

Closed
@F43nd1r

Description

@F43nd1r

Describe the bug
I'm facing an issue with subpath imports (as used in aws-jwt-verify) in combination with yarn berry and esbuild.
Build fails with

X [ERROR] Could not resolve "#node-web-compat"

    .yarn/cache/aws-jwt-verify-npm-3.1.0-1694314cd9-b978817018.zip/node_modules/aws-jwt-verify/dist/esm/jwt-rsa.js:8:30:
      8 │ import { nodeWebCompat } from "#node-web-compat";
        ╵                               ~~~~~~~~~~~~~~~~~~

  You can mark the path "#node-web-compat" as external to exclude it from the bundle, which will
  remove this error.

Workaround is to use a different linker mode in yarn, e.g. pnpm

To Reproduce
Reproducer can be found here.

Note: This issue was also reported here, but it seems more related to esbuild.

Activity

evanw

evanw commented on Sep 13, 2022

@evanw
Owner

Thanks for the report. It looks like the code that handles subpath imports and the code that handles Yarn PnP are probably just in the wrong order. I will fix this in the next release.

evanw

evanw commented on Sep 13, 2022

@evanw
Owner

Wait, does Yarn actually even allow this? I tried writing a test for this but Yarn throws this error when I run the test code natively with Yarn:

Error: aws-jwt-verify tried to access #node-web-compat, but it isn't declared in its dependencies; this makes the require call ambiguous and unsound.

Required package: #node-web-compat (via "#node-web-compat/package.json")
Required by: aws-jwt-verify@npm:3.1.0 (via .yarn/cache/aws-jwt-verify-npm-3.1.0-1694314cd9-b978817018.zip/node_modules/aws-jwt-verify/dist/esm/jwt-rsa.js)

If Yarn doesn't allow this, then maybe esbuild shouldn't either? Or maybe this is a bug with Yarn that should be fixed? Here's a simple way to reproduce this:

$ yarn init -2
$ yarn set version 3.2.3
$ yarn config set pnpEnableEsmLoader true
$ yarn add aws-jwt-verify@3.1.0
$ echo 'import "aws-jwt-verify"' > in.mjs
$ yarn node in.mjs
.pnp.cjs:8466
  return Object.defineProperties(new Error(message), {
                                 ^

Error: aws-jwt-verify tried to access #node-web-compat, but it isn't declared in its dependencies; this makes the require call ambiguous and unsound.

Required package: #node-web-compat (via "#node-web-compat/package.json")
Required by: aws-jwt-verify@npm:3.1.0 (via .yarn/cache/aws-jwt-verify-npm-3.1.0-1694314cd9-b978817018.zip/node_modules/aws-jwt-verify/dist/esm/jwt-rsa.js)

    at makeError (.pnp.cjs:8466:34)
    at resolveToUnqualified (.pnp.cjs:9385:21)
    at Object.resolveToUnqualified (.pnp.cjs:9529:26)
    at resolve$1 (.pnp.loader.mjs:224:31)
    at ESMLoader.resolve (node:internal/modules/esm/loader:530:30)
    at ESMLoader.getModuleJob (node:internal/modules/esm/loader:251:18)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:79:40)
    at link (node:internal/modules/esm/module_job:78:36)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)

Edit: Marking this issue as unactionable for now since esbuild is behaving the same way as Yarn, and therefore esbuild's current behavior is "expected". The tests for esbuild's implementation of Yarn PnP assert that esbuild's behavior matches Yarn since Yarn is the reference implementation, so this behavior is currently unable to be changed on esbuild's end because tests would start failing.

F43nd1r

F43nd1r commented on Sep 13, 2022

@F43nd1r
Author

Seems like this is an open issue in yarn, sorry. I didn't find it at first because it doesn't call the feature "subpath imports".

(also unrelated: damn, you're fast 😁)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      Participants

      @evanw@F43nd1r

      Issue actions

        Could not resolve "#node-web-compat" - subpath imports with esbuild and yarn pnp · Issue #2545 · evanw/esbuild