The .env file holds environment variables like API keys or database URLs. This keeps sensitive info out of the code and allows easy changes per environment.
import dotenv from 'dotenv'; dotenv.config(); console.log(process.env.API_KEY);
dotenv.config() loads variables from .env into process.env. So process.env.API_KEY will be '12345'.
In ES modules, you can import the named export 'config' and call it directly. Option A uses correct ES module syntax.
import dotenv from 'dotenv'; console.log(process.env.MY_VAR); dotenv.config();
dotenv.config() loads variables into process.env. If you access process.env before calling config(), variables are not loaded yet.
import dotenv from 'dotenv'; dotenv.config({ path: '.env' }); dotenv.config({ path: '.env.local' }); console.log(process.env.VAR);
dotenv.config() loads variables into process.env but does not overwrite existing keys by default (override: false). The first call sets VAR='first'. The second call finds VAR already set and skips it.
