vite-plugin-node-polyfills icon indicating copy to clipboard operation
vite-plugin-node-polyfills copied to clipboard

Failed to resolve import "vite-plugin-node-polyfills/shims/buffer"

Open OliverWich opened this issue 1 year ago • 10 comments

When using a version higher than 0.17.0, and polyfilling a bunch of stuff (see vite config snippet below), I get the following error: image

The relevant section of my vite config is this:

"plugins": [
// ...
 nodePolyfills({
      globals: {
        Buffer: true,
        global: true,
        process: true,
      },
      include: [
        'fs',
        'path',
        'crypto',
        'stream',
        'util',
        'http',
        'https',
      ],
    }),
// ...
]

With this config everything works fine in 0.17.0 but in the testet versions 0.19.0 and 0.21.0 the error occurs

When trying to include "buffer" in the include array, it also fails because there is no polyfill for it (or it gets already polyfilled by the globals section above). ( ERROR: [plugin: node-stdlib-browser-alias] Cannot find module 'vite-plugin-node-polyfills/shims/buffer' from '.')

Upvote & Fund

  • I'm now using Polar.sh, so you can upvote and help fund this issue.
  • I receive the funding once the issue is completed and you have confirmed it.
  • Thank you in advance for helping to prioritize and fund my work.
Fund with Polar

OliverWich avatar Feb 13 '24 17:02 OliverWich

Just got bit by this one on v0.21.0 @davidmyersdev this is a gnarly regression, could we get a fix in por favor?

FWIW this is due to the class node module resolution not finding anything at vite-plugin-node-polyfills/shims/buffer because the built files exist at vite-plugin-node-polyfills/shims/buffer/dist

shellscape avatar Feb 28 '24 20:02 shellscape

Similar issue here, version 0.17.0 works though.

raduciobanu22 avatar Mar 12 '24 04:03 raduciobanu22

Hello, also, I got same issue. I accepted my senior's comment. just change version to "0.17.0". And it works. Thank you bros.

choiwonpyo avatar Mar 14 '24 08:03 choiwonpyo

Um... Something happens.

I can build with latest version. (not 0.17.0) when I add "vite-plugin-node-polyfills" at the "top-level" of monorepo.

my project status is.

  • Using yarn berry. (PnP zero install)
  • monorepo
    • apps/~
    • packages/~
  • apps/client using "vite"
  • using turborepo for managing 'monorepo'

before "vite", I was developing with "cra".

Because I'm amature yet, I can not pick the reason why this situation happens...

  • As Above comment, And scan that repo, pnpm user seems to have Same issue. (not only yarn berry)

Situation

    1. I just "yarn add -D ~" at the "top level of monorepo" by mistake.
    1. After, I move to "apps/client" dir, And command "yarn add -D ~".
    1. two package.json file has "vite-plugin-node-polyfills" dependency.
    1. yarn turbo build. -> client build successfully.
    1. "why success??" -> I just remove the top level dependency. and yarn install at the top level.
    1. command yarn turbo build -> client build fail.

After that experience, I have some test.

I don't know why this solve the problem...

  • monorepo top level dependency, solve the problem.

choiwonpyo avatar Mar 14 '24 08:03 choiwonpyo

Running into this issue as well -- it occurs from within a lerna repo and a common package is referencing Buffer (but not using vite).

tommymarshall avatar Mar 21 '24 15:03 tommymarshall

Hello, also, I got same issue. I accepted my senior's comment. just change version to "0.17.0". And it works. Thank you bros.

This

17Amir17 avatar Apr 11 '24 15:04 17Amir17

Changing the version to 0.17.0 helps me as well. Thank you @raduciobanu22 🙏

KateAstrid avatar Jun 08 '24 08:06 KateAstrid

I wasn't able to reproduce this error with just the config provided above. I'll need a StackBlitz or CodeSandbox example in order to diagnose the issue and get a fix together.

davidmyersdev avatar Jul 04 '24 23:07 davidmyersdev

Happens for me when polyfilling crypto on v0.22.0, and the import comes from the ethers package:

Rollup failed to resolve import "vite-plugin-node-polyfills/shims/global" from "app/node_modules/ethers/lib.esm/crypto/crypto-browser.js".

As with others, downgrading to v0.17.0 resolves the issue.

blake-regalia avatar Jul 06 '24 21:07 blake-regalia

Same here. Using Quasar/Vite. Needed to go back to version ~0.17.0

Flamenco avatar Jul 12 '24 18:07 Flamenco

Going back to v0.17.0 clears the Buffer error, but gives me this:

TypeError: Cannot destructure property 'Uint8Array' of 'globalThis' as it is undefined.
    at node_modules/.pnpm/[email protected]_patch_hash=zkkuxompt5d553skpnegwi5wuy/node_modules/buffer/index.js (helpers.ts:226:31)
    at __require (helpers.ts:13:60)
    at shims/dist/index.js (helpers.ts:1919:46)
    at __init (helpers.ts:10:66)
    at shims/banner/index.cjs (helpers.ts:1927:13)
    at __require (helpers.ts:13:60)
    at helpers.ts:1933:5
    at helpers.ts:1934:3

Also, I would like to note that this error and the Buffer not foun only occur in vite dev. The built application works as expected.

GiovaniGuizzoYUB avatar Aug 27 '24 13:08 GiovaniGuizzoYUB

For those of you coming here with a problem with Buffer or any other polyfill, maybe this will help you:

Import the plugin before your react or vue plugin in the Vite config file. For some reason, if I import it after, it won't work.

So this will work:

plugins: [nodePolyfills(), react(), TanStackRouterVite()],

but this won't:

plugins: [react(), TanStackRouterVite(), nodePolyfills()],

GiovaniGuizzoYUB avatar Aug 27 '24 17:08 GiovaniGuizzoYUB

We've just updated to 0.22.0 and using nodePolyfills() as the first vite plugin as @GiovaniGuizzoYUB suggested seems to work fine. (And makes a ton of sense if you think about it :D)

I'll close this now, as it is fixed for us, but for some, downgrading to 0.17.0 might still be a valid "fix".

OliverWich avatar Sep 02 '24 14:09 OliverWich

With Quasar, including the plugin v 0.22 first still does not work.

quasar.config.ts

{
  vitePlugins: [
  [nodePolyfills, {include: ['buffer', 'path']}, {server: false}],
   // ...
}
[vite]: Rollup failed to resolve import "vite-plugin-node-polyfills/shims/buffer" from "/Users/steven/dev/projects/ct/ct-urm/src/v3/plugins/resource/platformResourceProvider.ts".
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`

Flamenco avatar Sep 02 '24 16:09 Flamenco

I was able to fix this by monkey-patching the Vite plugin

const nodePolyfillsFix = (options?: PolyfillOptions | undefined): Plugin => {
  const origPlugin = nodePolyfills(options)
  return {
    ...origPlugin,
    resolveId(this, source: string, importer: (string | undefined), opts: any) {
      const m = /^vite-plugin-node-polyfills\/shims\/(buffer|global|process)$/.exec(source)
      if (m) {
        return `node_modules/vite-plugin-node-polyfills/shims/${m[1]}/dist/index.cjs`
      } else {
        if (typeof origPlugin.resolveId === 'function') {
          return origPlugin.resolveId.call(this, source, importer, opts)
        }
      }
    }
  }
}

Or more simply because the plugin does not currently define resolveId.

const nodePolyfillsFix = (options?: PolyfillOptions | undefined): Plugin => {
  return {
    ...nodePolyfills(options),
    resolveId(source: string) {
      const m = /^vite-plugin-node-polyfills\/shims\/(buffer|global|process)$/.exec(source)
      if (m) {
        return `node_modules/vite-plugin-node-polyfills/shims/${m[1]}/dist/index.cjs`
      }
    }
  }
}

Flamenco avatar Sep 02 '24 16:09 Flamenco

I still suffer this issue with v0.22.0 and changing the sort of plugins does not work for me.

way2ex avatar Oct 14 '24 08:10 way2ex