Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ReferenceError: process is not defined #99

Closed
REX500 opened this issue May 1, 2023 · 3 comments
Closed

ReferenceError: process is not defined #99

REX500 opened this issue May 1, 2023 · 3 comments

Comments

@REX500
Copy link

REX500 commented May 1, 2023

Cannot use sdk's get method on Vite @^3.0.8 as it defaults to process.env.EDGE_CONFIG.
Instead it should accept a parameter for edge config, OR support Vite bundler.

Code example:

import { get } from '@vercel/edge-config';

export const config = { matcher: '/welcome' };

export function middleware(): Promise<any> {
	try {
		return get('greeting').then((res: any) => res.json());
	} catch (error) {
		console.log(error);
		return Promise.resolve(false);
	}
}

Error when trying to call middleware function:

ReferenceError: process is not defined

I'm guessing it happens when we try to read EDGE_CONFIG var key in the init function in index.edge.ts file:

// lazy init fn so the default edge config does not throw in case
// process.env.EDGE_CONFIG is not defined and its methods are never used.
function init(): void {
  // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
  if (!defaultEdgeConfigClient) {
    defaultEdgeConfigClient = createClient(process.env.EDGE_CONFIG);
  }
}

How to reproduce:

  • create a new project with Vite
  • install vercel's edge config sdk
  • paste the middleware code I posted above
  • call it
  • see the error

Proposed fix for the init function:
Update the init function:

// lazy init fn so the default edge config does not throw in case
// process.env.EDGE_CONFIG is not defined and its methods are never used.
function init(edgeConfig: string): void {
  // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
  if (!defaultEdgeConfigClient) {
    defaultEdgeConfigClient = createClient(edgeConfig);
  }
}

Update the get function and so on.... Ill gladly make a PR with the fix if you guys think it makes sense.

@dferber90
Copy link
Collaborator

That's a tricky one!

We'd still like to fall back to process.env.EDGE_CONFIG and have it as the default. This is what enables people to do

import {get} from @vercel/edge-config

instead of

import {createClient} from @vercel/edge-config
createClient().get()

How does vite usually deal with accessing the env from a package? Is there maybe anything we could do on that side?

@tcc-sejohnson
Copy link
Collaborator

tcc-sejohnson commented May 3, 2023

@REX500

The solution for Vite is to construct the client using your connection string:

import { createClient } from '@vercel/edge-config';
const client = createClient(/* your connection string */);
// client now has access to `get` and the other methods

@tcc-sejohnson
Copy link
Collaborator

Closing this one as there's no bug here, but I did create a PR to better-document the setup process for Vite: #117

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants