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

Env variables undefined in build but fine in dev #210

Closed
NavaceSystem opened this issue Mar 22, 2023 · 1 comment
Closed

Env variables undefined in build but fine in dev #210

NavaceSystem opened this issue Mar 22, 2023 · 1 comment

Comments

@NavaceSystem
Copy link

Have you experienced this bug with the latest version of the template?

Yes

Steps to Reproduce

I'm trying to copy some code over from another bare bones remix setup to the indie-stack. It all works fine in dev but after doing npm run build && npm start, the env variable SESSION_SECRET is coming out as undefined.

I want to be clear, the stack works in both dev and build if I don't touch anything but I've found something quite puzzling as a result of trying to hunt down the issue.

I've attached a repro with the below setup if you want to the last few steps and run the commands to replicate the issue.

Actual steps to reproduce:

  • Install the stack
  • Delete all the routes files except index.tsx and join.tsx
  • Delete session.server.ts and just its 1 remaining import from root.tsx
  • Change the routes/index.tsx file to just return <div>hello</div>.
  • Paste the below code into routes.join.tsx. All we're doing is logging process.env.SESSION_SECRET, or any other variable you want to put in .env.
import type { V2_MetaFunction } from "@remix-run/node";
import { getUserByEmail } from "~/models/user.server";

export const loader = () => {
  console.log("loader", process.env.SESSION_SECRET);
  return 0;
};

export async function action() {
  await getUserByEmail("stuff");
}

export const meta: V2_MetaFunction = () => [{ title: "Sign Up" }];

export default function Join() {
  return <div>join</div>;
}
  • Build and run the app using npm run build && npm start.
  • Notice the secret will be printed correctly in your terminal.
  • Next comment you this line await getUserByEmail("stuff"); in routes/join.tsx and build+run the app again.
  • You'll see the secret is coming out undefined.

I have tried this setup from scratch a dozen times with different variations and I can replicate the issue. It comes down to the fact that we need to import and call something from ~/models/user.server file in a route. You can try the same thing with other routes but it's the same thing.

I thought I had missed something but I have tried so many times and I don't see it.

remix-bugged.zip

Expected Behavior

Secrets should be available in build.

Actual Behavior

Secrets are not available in build.

@mcansh
Copy link
Contributor

mcansh commented Mar 22, 2023

we only load your .env file during development, if you're hosting your app where they don't have a way to load environment variables (vercel, fly, railway, etc) or are trying to run your production app locally, you'll need to update your start script in package.json to include dotenv/config (ex: binode --require dotenv/config -- @remix-run/serve:remix-serve build)

the reason it works OOTB is because Prisma is also autoloading your .env file for the prisma schema, but to me, it seems like they should first load the schema, find your environment variable you're trying to use and only inject that one.

dupe of #208

@mcansh mcansh closed this as not planned Won't fix, can't repro, duplicate, stale Mar 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants