Loading lesson path
Environment variables are dynamic named values that can affect how running processes behave on a computer. They are part of the environment in which a process runs and are used to configure applications without changing the code.
Accessing Environment Variables in Node.js Node.js provides the process.env object to access environment variables. This object contains all the environment variables available to the current process.
// Access a single environment variable const nodeEnv = process.env.NODE_ENV || 'development';
console.log(`Running in ${nodeEnv} mode`);
// Access multiple variables with destructuring const { PORT = 3000, HOST = 'localhost' } = process.env;
console.log(`Server running at http://${HOST}:${PORT}`);
// Check if running in production if (process.env.NODE_ENV === 'production') {
console.log('Production optimizations enabled');// Enable production features
}NODE_ENV Current environment (development, test, production) production
Port number for the server to listen on 3000