To do this, replace your file dotenv-example.jswith: 11-digit phone number format philippines JavaScript Copy the code console.log('The value for FOO is:', process.env.FOO); Then run the file as you normally would: JavaScript Copy the code node dotenv-example.js the current value of FOOis undefined. Now run it with the appropriate flag to call dotenv : JavaScript Copy the code node -r dotenv/config dotenv-example.

js The result is that FOOis now set to barsince the file .envwas loaded. If you want to know more about dotenv, check out its documentation. Load .env files differently There is also another module based on dotenv to make loading environment variables easier. The result is node-env-run or nodenv. This command line tool will load an .
Initialize the values using dotenv, and then run your script. You can install it globally, but this is only recommended for development purposes and at the local project level. Install it by running: JavaScript Copy the code npm install node-env-run --save-dev Then create a file nodenv-example.jsand insert this code into it: JavaScript Copy the code console.