Environment variables help keep secret keys and settings safe and separate from your code. They let your app change behavior without changing code.
Environment variable management in Remix
process.env.VARIABLE_NAME
Use process.env to access environment variables in Remix server code.
Variables must be defined in a .env file or your hosting environment.
const apiKey = process.env.API_KEY;
const nodeEnv = process.env.NODE_ENV || 'development';if (process.env.FEATURE_FLAG === 'true') { // enable feature }
This Remix loader reads the API_URL environment variable and passes it to the component via loader data. It shows how to provide a default if the variable is missing.
import { json } from '@remix-run/node'; import { useLoaderData } from '@remix-run/react'; export const loader = async () => { const apiUrl = process.env.API_URL || 'https://default.api'; return json({ apiUrl }); }; export default function EnvExample() { const { apiUrl } = useLoaderData(); return ( <main> <h1>Environment Variable Example</h1> <p>API URL: {apiUrl}</p> </main> ); }
Remember to restart your Remix server after changing environment variables.
Never expose secret environment variables to client-side code directly.
Use .env files for local development and set variables in your hosting platform for production.
Environment variables keep secrets and settings outside your code.
Access them in Remix using process.env.VARIABLE_NAME.
Always provide defaults and avoid exposing secrets to the browser.
Practice
Solution
Step 1: Understand environment variables role
Environment variables hold sensitive data or settings outside the code to keep them secure and flexible.Step 2: Compare options with purpose
Only To store secrets and configuration outside the code matches this purpose; others relate to styling, sessions, or routing which are unrelated.Final Answer:
To store secrets and configuration outside the code -> Option AQuick Check:
Environment variables = secrets/settings outside code [OK]
- Thinking environment variables control styling
- Confusing environment variables with session management
- Assuming environment variables handle routing
API_KEY in a Remix loader function?Solution
Step 1: Recall Remix environment variable syntax
In Remix server code, environment variables are accessed viaprocess.env.VARIABLE_NAME.Step 2: Check each option
const key = process.env.API_KEY; usesprocess.env.API_KEY, which is correct. Others use invalid or client-only patterns.Final Answer:
const key = process.env.API_KEY; -> Option BQuick Check:
Use process.env.VARIABLE_NAME in Remix server code [OK]
- Using client-side env access like import.meta.env in server code
- Trying to access env variables from window object
- Using undefined variables like env.API_KEY
apiUrl if process.env.API_URL is undefined?
export async function loader() {
const apiUrl = process.env.API_URL || 'https://default.api.com';
return { apiUrl };
}Solution
Step 1: Understand the || operator behavior
Ifprocess.env.API_URLis undefined (falsy), the expression returns the right side string.Step 2: Apply to given code
Sinceprocess.env.API_URLis undefined,apiUrlbecomes 'https://default.api.com'.Final Answer:
'https://default.api.com' -> Option CQuick Check:
Falsy env var uses default string [OK]
- Expecting undefined instead of default string
- Thinking it throws an error if env var missing
- Confusing null with undefined
export function loader() {
return { apiKey: process.env.SECRET_API_KEY };
}
export default function Component() {
const { apiKey } = useLoaderData();
return <div>API Key: {apiKey}</div>;
}Solution
Step 1: Understand environment variable exposure
Secrets like API keys should never be sent to the client because it exposes sensitive data.Step 2: Analyze the code behavior
The loader returns the secret key, which the component renders, exposing it in the browser.Final Answer:
Exposing SECRET_API_KEY to client is a security risk -> Option DQuick Check:
Never send secrets to client [OK]
- Thinking loader cannot access env vars
- Believing useLoaderData can't access loader data
- Assuming async is required for all loaders
Solution
Step 1: Understand Remix environment variable conventions
Remix exposes environment variables prefixed withPUBLIC_to client code safely; secrets remain server-only.Step 2: Evaluate options for security and access
Prefix public keys withPUBLIC_and access them in client code; keep secrets without prefix and access only in server code follows this pattern, separating public and secret keys correctly. Others expose secrets or use insecure methods.Final Answer:
Prefix public keys with PUBLIC_ and access them in client code; keep secrets without prefix and access only in server code -> Option AQuick Check:
Use PUBLIC_ prefix for client env vars [OK]
- Exposing all keys to client without prefix
- Storing secrets in JSON files accessible to client
- Using window.env which is not standard
Ready to prove it?
Pass the test and this pattern moves unknown
