Environment Variables Missing `npm run start`

All of my environment variables are missing or undefined after running npm run build and npm run start. npm run dev works as expected. Does the build have access to the .env file at the root of the project? If not, how do I access them in the build?
8 Replies
Brendonovich
Brendonovich3mo ago
Environment variables at runtime depend on where you're deploying your code. With the default node-server preset you'll need to load your envs at runtime with something like dotenv
ChrisThornham
ChrisThornhamOP3mo ago
I didn't have to do that with SolidStart 0.5.9. Did that change with 1.0? Also, you said: "With the default node-server preset, you'll need to load your envs at runtime with something like dotenv" Are there any references you can point me to? For context... I'm just trying to run npm start on my local computer for now. And I'm trying to access the variables with process.env.API_KEY when I'm using "use server";
Brendonovich
Brendonovich3mo ago
I didn't have to do that with SolidStart 0.5.9. Did that change with 1.0?
Not sure soz, maybe the old version loaded env files automatically
Are there any references you can point me to?
Nah, there should probably be something about it in the docs
And I'm trying to access the variables with process.env.API_KEY when I'm using "use server";
Yeah Nitro doesn't automatically load env files with the node-server preset (issue), perhaps it should but for now adding dotenv in entry-server might work
ChrisThornham
ChrisThornhamOP3mo ago
Ok... just to be clear, you said: Nah, there should probably be something about it in the docs Which docs? SolidStart, dotenv, Nitro, Vite?
Brendonovich
Brendonovich3mo ago
I think technically it's a Nitro responsibility, but for sake of people just trying out Start there should probably be a section in our docs about it
ChrisThornham
ChrisThornhamOP3mo ago
Haha. I agree. It would be helpful if it was covered in the Start docs. Thanks for the help. I'll keep digging into it. I just deployed a temp site to Vercel and added my variables, and it works. I might have to use that method for now. I'm 4-5 hours into this, and I've gotten nowhere.
Brendonovich
Brendonovich3mo ago
have you tried adding dotenv in entry-server?
npm
dotenv
Loads environment variables from .env file. Latest version: 16.4.5, last published: 7 months ago. Start using dotenv in your project by running npm i dotenv. There are 49021 other projects in the npm registry using dotenv.
ChrisThornham
ChrisThornhamOP3mo ago
Sorry, I missed this. Yes, I tried that as well, and it didn't work.
Want results from more Discord servers?
Add your server