Performance: Environment configuration
This affects the startup time and memory usage of Node.js applications by controlling how environment variables and configuration files are loaded.
if (process.env.NODE_ENV !== 'production') { require('dotenv').config({ path: './small-config.env' }); } // load only small dev config
require('dotenv').config({ path: './large-config.env' }); // large file with many variables loaded synchronously
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Synchronous .env load with large file | N/A | N/A | N/A | [X] Bad |
| Conditional small .env load | N/A | N/A | N/A | [OK] Good |
| Repeated process.env access in loops | N/A | N/A | N/A | [X] Bad |
| Cached environment variables | N/A | N/A | N/A | [OK] Good |
| Synchronous JSON config read | N/A | N/A | N/A | [X] Bad |
| Native JSON import or async read | N/A | N/A | N/A | [OK] Good |
.env file using the dotenv package in Node.js?require('dotenv').config() at the start of your app..env file with PORT=4000, what will be the output?
require('dotenv').config();
console.log(process.env.PORT || 3000);require('dotenv').config() loads PORT=4000 into process.env.PORT.process.env.PORT || 3000 uses the loaded value 4000, so it prints 4000.import dotenv from 'dotenv'; dotenv.config; console.log(process.env.DB_HOST);
dotenv.config without parentheses, so the config function is not executed.config(), environment variables from .env are not loaded, so process.env.DB_HOST may be undefined..env file keeps them out of code and version control.dotenv loads the key into process.env, which the app can safely use.Pass the test and this pattern moves unknown