esbuild-plugins-node-modules-polyfill icon indicating copy to clipboard operation
esbuild-plugins-node-modules-polyfill copied to clipboard

Plugin not working, not pollyfilling

Open BowgartField opened this issue 7 months ago • 4 comments

Issue description

Hi, I'm trying to use this plugin in my esbuild bundling file. But it seems like it is not working. I got this error:

1 warning and 47 errors
Build failed: Error: Build failed with 47 errors:
node_modules/@etherspot/prime-sdk/node_modules/ws/lib/permessage-deflate.js:3:21: ERROR: Could not resolve "zlib"
node_modules/@etherspot/prime-sdk/node_modules/ws/lib/receiver.js:3:29: ERROR: Could not resolve "stream"
node_modules/@etherspot/prime-sdk/node_modules/ws/lib/sender.js:3:35: ERROR: Could not resolve "crypto"
node_modules/@etherspot/prime-sdk/node_modules/ws/lib/stream.js:3:27: ERROR: Could not resolve "stream"
node_modules/@etherspot/prime-sdk/node_modules/ws/lib/websocket-server.js:4:31: ERROR: Could not resolve "crypto"

Do i do something wrong ? Esbuild plateform is "browser" and "node". If i put "node" error goes awayt but it caused other problem in my bundle.

Thanks.

Code sample

Code is here:
https://github.com/BowgartField/esbuild-polyfill-bug-report

Package version

1.7.0

Node.js version

v22.0.0

Operating system

macos

I have tested this issue on a next release

No response

BowgartField avatar Jun 05 '25 13:06 BowgartField

seems to work by replacing: mainFields: ["module", "main"], to mainFields: ["browser", "module", "main"],.

But still error:

$ node esbuild.mjs
✘ [ERROR] Could not resolve "node:process"

    node_modules/esbuild-plugins-node-modules-polyfill/globals/process.js:1:35:
      1 │ export { default as process } from 'node:process';
        ╵                                    ~~~~~~~~~~~~~~

  The package "node:process" wasn't found on the file system but is built into node. Are you trying
  to bundle for node? You can use "platform: 'node'" to do that, which will remove this error.

▲ [WARNING] The glob pattern import("./**/*.entry.js") did not match any files [empty-glob]

    node_modules/@metamask/sdk/dist/browser/es/metamask-sdk.js:6:0:
      6 │ `./${i}.entry.js`).then((e=>(bf.set(i,e),e[r])),vf)})(n);if(e&&"then"in e){const t=()=>{};i=await e,t()}else i=e;if(!i)throw new Error(`Constructor for "${n.$tagName$}#${t.$modeName$}" was...
        ╵ ~~~~~~~~~~~~~~~~~

1 warning and 1 error
Build failed: Error: Build failed with 1 error:
node_modules/esbuild-plugins-node-modules-polyfill/globals/process.js:1:35: ERROR: Could not resolve "node:process"
    at failureErrorWithLog (/Users/jean-benoit/Documents/realt/esbuild-polyfill-bug-repot/node_modules/esbuild/lib/main.js:1476:15)
    at /esbuild-polyfill-bug-repot/node_modules/esbuild/lib/main.js:945:25
    at //esbuild-polyfill-bug-repot/node_modules/esbuild/lib/main.js:1354:9
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
  errors: [Getter/Setter],
  warnings: [Getter/Setter]
}

BowgartField avatar Jun 05 '25 13:06 BowgartField

I'll look into it with the repro

imranbarbhuiya avatar Jun 05 '25 16:06 imranbarbhuiya

You need browser to tell esbuild to use the browsers path from package json instead of requiring from node. The node:process error you are getting is because you didn't added process in modules. It's present in globals but some package is importing from node:process module instead of global one so you need to add process in modules too. I've tested and seems like it's working, try and lmk if it fixes it for you

imranbarbhuiya avatar Jun 11 '25 14:06 imranbarbhuiya

I notice that the resolving logic is surprisingly bypassed as soon as you add the browser field in the package.json. For a browser-oriented build this is impossible to avoid.

Here is a minimal reproduction: https://github.com/qbtf/esbuild-polyfill-for-browser-issue

Note that the build can be fixed by removing that field.

qbtf avatar Jul 08 '25 07:07 qbtf