davidmyersdev / vite-plugin-node-polyfills

A Vite plugin to polyfill Node's Core Modules for browser environments.
MIT License
263 stars 17 forks source link

Error: Cannot find module 'process/browser.js' #32

Closed MichailShcherbakov closed 10 months ago

MichailShcherbakov commented 10 months ago

Hi @davidmyersdev.

When I run yarn I get a waring

Screenshot_2

And when I try to build a lib vite throws an error

Screenshot_3

Link to Codesandbox

Can you help me to fix it?

Thank you!

manylovv commented 10 months ago

I've got the same error

manylovv commented 10 months ago

I decided to try another version, and ^0.7.0 works just fine for me

MichailShcherbakov commented 10 months ago

@manylovv Unfortunately, I can't use 0.7.0 version in my project. I need "exclude" functionality.

It looks like the error is in "buffer-polyfill" in the package.json. Yarn possibly does not support it.

I don't know why the `shim.js' file is used here

const globalShimsPath = require.resolve('vite-plugin-node-polyfills/shims')

instead of using the helpers/esbuild/shim.js file from node-stdlib-browser

const globalShimsPath = require.resolve('node-stdlib-browser/helpers/esbuild/shim')

and for what this one

included[name] = globals.includes(name) ? globalShimsPath : value

I just changed all of it. And it works fine for me.

davidmyersdev commented 10 months ago

I've started looking into this, but I'm having trouble reproducing it locally.

MichailShcherbakov commented 10 months ago

@davidmyersdev I've attached a link to an example project. The error is reproduced when building or running project after yarn install.

martonlederer commented 10 months ago

Same here

davidmyersdev commented 10 months ago

I think I see what's happening now. It appears that Yarn v1 does not handle the aliased process dependency as it should, so it just skips it rather than unpack it into node_modules. I'll have to figure out how I want to handle this, but it shouldn't be too bad to get a fix together. To answer your earlier question, the replacement shims file is necessary because node-stdlib-browser has an out-of-date buffer dependency.

davidmyersdev commented 10 months ago

There also appears to be an open issue about this bug without any responses. https://github.com/yarnpkg/yarn/issues/6907

davidmyersdev commented 10 months ago

This is fixed in v0.11.3.

martonlederer commented 10 months ago

Thank you @davidmyersdev