server env variables cause process is not defined
Wasp version - 0.12.3
Root .env.server with sendgrid api key
Chrome macos
18 Replies
Hi @marti. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
----------------------
The error
Uncaught ReferenceError: process is not defined
typically occurs when you're trying to access Node.js specific variables or modules in the client-side code. In Wasp, environment variables can be used in both the client and the server code, but they are handled differently.
For server-side code, you can directly use process.env.YOUR_VARIABLE
to access environment variables. However, for client-side code, you need to prefix your environment variables with REACT_APP_
and access them using import.meta.env.REACT_APP_YOUR_VARIABLE
.
If you're seeing this error, it's likely that you're trying to access a server environment variable in your client-side code without the REACT_APP_
prefix.
Please ensure that you're using the correct prefix and accessing method based on where you're using the environment variables.
For more information, you can refer to the Wasp documentation on environment variables (https://wasp-lang.dev/docs/project/env-vars).
If you're still facing issues, it would be helpful to know more about where and how you're trying to use these environment variables.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------
Sorry @marti, I seem to be having a bit of trouble. Don't worry though, I've pinged my support and they will try to fix me up! :technologist:
Perhaps try again in 15mins?Please take a screenshot of the error or copy paste the error so we can better understand what is going on π
The error exactly is what I said above.
Uncaught reference error: process is not defined.
And if I go into the error it is where sendgrid api key is used.
Const emailProvider = {
Type: βsendgridβ,
ApiKey: process.env.SENDGRID_API_KEY,
} as const;
Some extra info would help me:
- Did you see this error in the terminal or browser console?
- When did this error appear? On start or somewhere along the way?
- Also, share any piece of code you can e.g. where you import
import { emailSender } from 'wasp/server/email'
- Are you trying to import this in a React component?ok sorry π
no terminal error, this error si from browser console
this happened after I replaced dummy email provider with sendgrid
so what I did was
creted
.env.server
in root with SENDGRID_API_KEY=
in main.wasp
I did this
and that's it, after I restarted the server I saw this err- Okay now we are getting somwhere π okay, so you took the OpenSaaS template and just replaced the email sender?
- Did you modify the code in any other way e.g. changed some of the React components or added new imports somewhere?
- Btw. if you are just starting out, I'd recommend upgrading to Wasp 0.13.1
curl -sSL https://get.wasp-lang.dev/installer.sh | sh -s
- Btw. if you had the Dummy
provider, you probably took the template code from the main
branch of the repository π because we didn't have the Dummy
provider in Wasp 0.12.3when I did this
curl -sSL https://get.wasp-lang.dev/installer.sh | sh -s -- -v 0.13.1
and when I do wasp start I see error
- I might just start over haha...I have not that manny stuff to c/pWohooo @marti, you just became a Waspeteer level 1!
Uncaught reference error: process is not defined.
coming from the browser console -> sounds like server code using process
is being imported into client code and therefore failing, since client
is not available in the browser?yes, that was my thought as well, but nowhere in the client is using
process
and when I track the err in console it leads me to wasp part ->
ApiKey: process.env.SENDGRID_API_KEY,
Did you modify the code in any other way e.g. changed some of the React components or added new imports somewhere?One thing for debug you can do: modify your
vite.config.ts
to look like this
And then start your app, let me know if you get a big red error in the browser πI tried upgrading wasp, but now I get err
π«
I got actually
And I followed the docs and did same as they did
import { emailSender } from "wasp/server/email";
So my sendEmail.ts
looks like
But apparently I am doing something wrong. Can you help me?Did you import the
sendEmail
action from wasp/server/operations
in your React component? πNo, I imported it from actions
import { sendEmail } from "../../actions/sendEmail";
Aw yes, you can't do that π you can't import stuff that's backend specific on the client
I'd recommend going through the Wasp tutorial to get the hang of the queries and actions in Wasp, but specifically this part: https://wasp-lang.dev/docs/tutorial/actions#invoking-the-action-on-the-client
6. Modifying Data | Wasp
In the previous section, we learned about using Queries to fetch data and only briefly mentioned that Actions can be used to update the database. Let's learn more about Actions so we can add and update tasks in the database.
Even though client and server files live alongside each other, there is still a network between them π Server code is running in Node.js and the client code in the browser. You need to use the
wasp/client/operations
import to invoke the actions properly. When you use that, a network call happens in the background calling the server from the client.
Calling the action directly, will try to execute server code in the browser which is no bueno and fails with the error you saw.thank you so much π