GetStream / stream-js

JS / Browser Client - Build Activity Feeds & Streams with GetStream.io
https://getstream.io
BSD 3-Clause "New" or "Revised" License
331 stars 111 forks source link

Error: process is not defined #570

Open harryfino opened 1 year ago

harryfino commented 1 year ago

I was using version 8.1.0 with no issues. I updated this morning to version 8.1.5 and I started getting this error: Error: process is not defined

It occurs when I call a feed's get() method.

const client = StreamConnect(API_KEY, accessToken, APP_ID);
const feed = client.feed('feedname', userId);
feed.get({ limit: 0 })
nusryan commented 1 year ago

hi @harryfino , unfortunately I'm also facing with this issue, this function works fine locally but I was receiving it when trying to build with Vite

johnvey commented 1 year ago

The offending change looks to be here: https://github.com/GetStream/stream-js/commit/8c20f111d0aa89d8307956310fec97f027d0db7b#diff-25d66d74617fe2e23d7946bd6e3ba95640ab1b9bc8947445d604fc271c7c1f12R399

The code looks like it was written with only server-side Node execution in mind, so any client-side execution that lacks the global process variable will throw this exception.

The workaround for Vite during dev time is to inject a stub in the config file like:

define: {
  'process.env': {}
}

However, that define option changes depending on when you run vite dev vs vite build, so you must use a different workaround for your builds. The simplest workaround is to stub something out in the <head> of your HTML like:

window.process = { env: {} };

If you already have an existing polyfill lib running, you can of course inject it that way.

You can see the equivalent Webpack workaround in the same commit here: https://github.com/GetStream/stream-js/commit/8c20f111d0aa89d8307956310fec97f027d0db7b#diff-1fb26bc12ac780c7ad7325730ed09fc4c2c3d757c276c3dacc44bfe20faf166fR42