Preparing your workspace
Environment variables allow applications to receive configuration from outside the source code.
Production applications should not hard-code sensitive or environment-specific values directly into source files. Node.js applications commonly read configuration such as PORT, DATABASE_URL, and API keys from environment variables. Different environments can then use different values without changing the application code. Sensitive credentials should never be committed to source control. It is also useful to validate required configuration during application startup so missing values fail early.
A learning platform can use one database URL locally, another in staging, and another in production without changing backend source code.
const port = Number(process.env.PORT ?? 3000);
const databaseUrl = process.env.DATABASE_URL;
if (!databaseUrl) {
throw new Error('DATABASE_URL is required');
}
console.log(`Server will use port ${port}`);The application reads the port and database configuration from the environment and stops early if the database URL is missing.