Home > Enterprise >  500 process is not defined ReferenceError: process is not defined
500 process is not defined ReferenceError: process is not defined

Time:08-14

I am getting this problem every time I import a lib or when I use puppeteer and I don't know how to fix it. I am trying to get some data from LinkedIn using https://www.npmjs.com/package/linkedin-client the code is easy:

import LinkedinClient from 'linkedin-client';
    async function getIt() {
        const session = supabase.auth.session();

        const tok = session?.provider_token;
        const token = JSON.stringify(tok);
        console.log(token);
        
        const client = new LinkedinClient(token);
        const data = await client.fetch('https://www.linkedin.com/in/some-profile/');
        console.log(data);
    }

at first it gives me this error:Module "util" has been externalized for browser compatibility. Cannot access "util.promisify" in client code

after I install npm i util then it displays the following error:

500 process is not defined ReferenceError: process is not defined

Can you please let me know how to fix it?(I'm using sveltekit)

CodePudding user response:

The library requires to be run on the server. It has be in a server endpoint, it cannot be in a component or a load function.

If this is already the case, this might be an issue with Vite trying to remove server dependencies. There is e.g. a plugin @esbuild-plugins/node-globals-polyfill which polyfills the process variable. It may also be necessary to list packages in resolve.alias in the Vite config, to point to the Node modules.

  • Related