davidmyersdev/vite-plugin-node-polyfills

Cannot find module 'vite-plugin-node-polyfills/shims/buffer'

shellscape opened this issue · 9 comments

Just updates to 0.19.0 from 0.16.0 and started getting the error Cannot find module 'vite-plugin-node-polyfills/shims/buffer

Downgraded back to 0.16.0 and error vanished. Apologies that I don't have the time atm to put together a reproduction, but it seems to affect every vite v4.x project I have going.

I am getting a similar error when upgrading from 0.17.0 to 0.18.0 and later. I am using vite@5.0.11.

Error:  Failed to scan for dependencies from entries:
  C:/xxx/frontend/index.html

  X [ERROR] No matching export in "node_modules/vite-plugin-node-polyfills/shims/buffer/dist/index.cjs" for import "Buffer"

    script:C:/xxx/frontend/src/components/renderers/typeHandlers/text/TextEditor.vue?id=0:17:9:
      17 │ import { Buffer } from 'node:buffer'

If either of you could add a reproduction on StackBlitz or similar, that'd be helpful for me to debug.

FWIW it's extremely helpful to have a template ready to go for issues. I've created these two for different projects as a reference: https://stackblitz.com/fork/jsx-email-repro https://stackblitz.com/fork/rollup-repro

I'm not sure how to isolate my use case for this into a repro for Vite, I just don't have enough experience with Vite yet to know how to force the behavior.

Here is the Stackblitz:
https://stackblitz.com/edit/stackblitz-starters-8ajb44?file=package.json

npm run dev:frontend

will yield:

Error:   Failed to scan for dependencies from entries:
  /home/projects/stackblitz-starters-8ajb44/frontend/index.html

  ✘ [ERROR] No matching export in "../node_modules/vite-plugin-node-polyfills/shims/buffer/dist/index.cjs" for import "Buffer"

    script:/home/projects/stackblitz-starters-8ajb44/frontend/src/App.vue?id=0:2:9:
      2 │ import { Buffer } from 'node:buffer'
        ╵          ~~~~~~


    at failureErrorWithLog (/home/projects/stackblitz-starters-8ajb44/node_modules/esbuild/lib/main.js:1641:15)
    at eval (/home/projects/stackblitz-starters-8ajb44/node_modules/esbuild/lib/main.js:1049:25)
    at runOnEndCallbacks (/home/projects/stackblitz-starters-8ajb44/node_modules/esbuild/lib/main.js:1476:45)
    at buildResponseToResult (/home/projects/stackblitz-starters-8ajb44/node_modules/esbuild/lib/main.js:1047:7)
    at eval (/home/projects/stackblitz-starters-8ajb44/node_modules/esbuild/lib/main.js:1059:9)
    at requestCallbacks.on-end (/home/projects/stackblitz-starters-8ajb44/node_modules/esbuild/lib/main.js:1058:54)
    at handleRequest (/home/projects/stackblitz-starters-8ajb44/node_modules/esbuild/lib/main.js:722:17)
    at handleIncomingPacket (/home/projects/stackblitz-starters-8ajb44/node_modules/esbuild/lib/main.js:747:7)
    at Socket.readFromStdout (/home/projects/stackblitz-starters-8ajb44/node_modules/esbuild/lib/main.js:670:7)

With this change, it will work:

npm add -D vite-plugin-node-polyfills@0.17.0 -w frontend
npm run dev:frontend

The error can also be removed by converting the package in the common workspace from "type": "commonjs" to "type": "module" (and changing the export syntax in common/src/index.js).

I have the same issue i have to downgrade to 0.17.0

"vite": "^5.0.3",
"vite-plugin-node-polyfills": "^0.17.0",

This seems to be related to some bigger changes happening in Vite 5. From what I can tell, version v5.0.5 is where things stopped working, although version v5.0.9 is where this specific error started happening. I have an idea for a fix, but I'm still investigating the root cause.

The fix for this has been released in v0.20.0. This fork of the reproduction above is now working with the latest version.
https://stackblitz.com/edit/stackblitz-starters-tkthj2?file=frontend%2Fvite.config.js,frontend%2Fpackage.json

mfal commented

Unfortunately when upgrading to v0.20.0 the Vite builder errors with

Error: [vite]: Rollup failed to resolve import "vite-plugin-node-polyfills/shims/buffer" from "/dev/test/.yarn/cache/@storybook-addon-interactions-npm-7.6.13-7b19f88dc9-75f04779d1.zip/node_modules/@storybook/addon-interactions/dist/preview.js".
This is most likely unintended because it can break your application at runtime.
If you do want to externalize this module explicitly add it to
`build.rollupOptions.external`

@mfal that sounds like a different issue. Can you please open a new issue with a reproduction? I tested the build with the above example, and it seems to be working fine.