Calen
Calen2mo ago

Error: Convex functions should not be imported in the browser.

My convex server looks like this:
'use node';
import { v } from 'convex/values';
import { action } from '../../_generated/server';
import { initProject } from '../../ai/codegen';

export const createProject = action({
args: {
prompt: v.string(),
model: v.string(),
},
handler: async (ctx, args) => {
const { prompt, model } = args;
const result = await initProject({
prompt,
model,
});
return result;
},
});
'use node';
import { v } from 'convex/values';
import { action } from '../../_generated/server';
import { initProject } from '../../ai/codegen';

export const createProject = action({
args: {
prompt: v.string(),
model: v.string(),
},
handler: async (ctx, args) => {
const { prompt, model } = args;
const result = await initProject({
prompt,
model,
});
return result;
},
});
I use it in react like so:
I use it in react like so:
import { useAction } from 'convex/react';
import { api } from '../../convex/_generated/api';
...

const initProject = useAction(api.actions.ai.actions.createProject);
import { useAction } from 'convex/react';
import { api } from '../../convex/_generated/api';
...

const initProject = useAction(api.actions.ai.actions.createProject);
When i start the react app i get this error:
throw new Error("Convex functions should not be imported in the browser.");
throw new Error("Convex functions should not be imported in the browser.");
Why do I get this and how can I fix it
18 Replies
Convex Bot
Convex Bot2mo ago
Thanks for posting in <#1088161997662724167>. Reminder: If you have a Convex Pro account, use the Convex Dashboard to file support tickets. - Provide context: What are you trying to achieve, what is the end-user interaction, what are you seeing? (full error message, command output, etc.) - Use search.convex.dev to search Docs, Stack, and Discord all at once. - Additionally, you can post your questions in the Convex Community's <#1228095053885476985> channel to receive a response from AI. - Avoid tagging staff unless specifically instructed. Thank you!
sshader
sshader2mo ago
That all looks right. Are there any other imports to stuff in your convex directory (aside from _generated/api? Does the error show a stack trace?
Calen
CalenOP2mo ago
I don't think theres any more relevant stuff in the convex dir. I am calling it in a component of https://github.com/expo/snack (website/src/client)
GitHub
GitHub - expo/snack: Expo Snack lets you run Expo in the browser.
Expo Snack lets you run Expo in the browser. Contribute to expo/snack development by creating an account on GitHub.
Calen
CalenOP2mo ago
This is the stacktrace
ballingt
ballingt2mo ago
This error is there to say "hey, careful, it looks like you're shipping Convex functions (your potentially secret backend) with your client" so somewhere you're importing code that includes a const list = query(...) in a file that runs in the browser hm I don't see the error you mentioned here though so maybe that's not what's happening
Calen
CalenOP2mo ago
Im confused. If my client is client site rendered I cannnot call:
const initProject = useMutation(api.actions.ai.codegen.initProjectStream);
const initProject = useMutation(api.actions.ai.codegen.initProjectStream);
?
ballingt
ballingt2mo ago
Where doyou see this error? I think this error isn't actually happening These logs indicate a different issue
If my client is client site rendered I cannnot call:
No that's fine
Calen
CalenOP2mo ago
So this is the whole file:
ballingt
ballingt2mo ago
Could you show a screenshot of this error? I'm wondering if you saw this error in these logs and then assumed the error was being thrown, but actually it's not being thrown that's just a line of source code
Calen
CalenOP2mo ago
When i comment out line 28:
// const initProject = useMutation(api.actions.ai.codegen.initProjectStream);
// const initProject = useMutation(api.actions.ai.codegen.initProjectStream);
it works just fine, if i uncomment it i get this error
ballingt
ballingt2mo ago
Your file is fine What is "this error," what is the error? The error I see in the logs you posted is "Failed to compile"
Calen
CalenOP2mo ago
ah i see what you mean now
ballingt
ballingt2mo ago
confusing because that line is visible, but the highlighted line is a couple lines off
Calen
CalenOP2mo ago
mhm i will try to figure it out, thanks a lot and sorry for the confusion
ballingt
ballingt2mo ago
it does seem like it's to do with this Convex code though looks like it's about the ? in there
Calen
CalenOP2mo ago
yeah convex uses react 18? the expo snack requires react 16.x.x
ballingt
ballingt2mo ago
so whatever your bunder setup is, it's having trouble with optional chaining stuff like const name = options?.name
sshader
sshader2mo ago
https://github.com/expo/snack/pull/302/ looks like it might be relevant here -- some issue with the custom packager expo/snack uses But also, Convex definitely works smoothly with Expo Go (https://docs.expo.dev/develop/tools/#expo-go) including the web view, in case you're open to using that instead of snacks
GitHub
[snackager] correct output with package type: module by mmomtchev ·...
Why Refs: #299 Refs: microsoft/rnx-kit#142 The Metro Babel preset does not produce correct output for packages containing &quot;type&quot;: &quot;module&quot; unless the disableImpo...
Expo Documentation
Tools for development
An overview of Expo tools and websites that will help you during various aspects of your project-building journey.

Did you find this page helpful?