why I am getting error: Convex functions should not be imported in the browser

I don’t understand what I have to pay attention to to fix this mistake
No description
3 Replies
erquhart
erquhart2y ago
It sounds like you're importing the function directly rather than through the api object
antonsadovskiy
antonsadovskiyOP2y ago
I checked, no, I import all functions through the api object
ballingt
ballingt2y ago
Based on this error message @antonsadovskiy it looks like window is defined when you run your convex functions. You might be using a library that defines it? As a workaround you could temporarily include code like
if (typeof window !== 'undefined') {
(window as any).__convexAllowFunctionsInBrowser = true;
}
if (typeof window !== 'undefined') {
(window as any).__convexAllowFunctionsInBrowser = true;
}
in the file where your Convex functions are defined above their definitions I'm curious about who is setting window, could be a polyfill — you might try console.log(window); to see We can make this check more cautious and allow window to exist if you're in the Convex environment — the point of the check is to prevent convex functions from being defined in a browser, because that could mean you're shipping potentially secret code in your browser bundle. We'll change this to more precisely detect a browser environment, would love to know what window is for you @antonsadovskiy so that we can be sure we're fixing this for you. Thanks for your help, for others reading the issue was something like
const dom = new JSDOM();
global.window = dom.window;
const dom = new JSDOM();
global.window = dom.window;
because defining global.window makes Convex think that the functions are in the browser, which could mean a security issue. But in this case this is fine! So we'll fix this, it's a false alarm from Convex.

Did you find this page helpful?