0
0
Node.jsframework~5 mins

process.env for environment variables in Node.js - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is process.env in Node.js?

process.env is an object in Node.js that holds environment variables as key-value pairs. It allows your app to access system or user-defined settings.

Click to reveal answer
beginner
How do you access an environment variable named PORT in Node.js?
<p>You access it using <code>process.env.PORT</code>. For example, <code>const port = process.env.PORT;</code></p>
Click to reveal answer
beginner
Why should sensitive data like API keys be stored in environment variables instead of code?

Storing sensitive data in environment variables keeps secrets out of your codebase. This helps protect them from being exposed if the code is shared or published.

Click to reveal answer
beginner
What happens if you try to access an environment variable that is not set?

If the variable is not set, process.env.VARIABLE_NAME returns undefined. You can provide a default value to avoid errors.

Click to reveal answer
intermediate
How can you set environment variables for a Node.js app when running it locally?

You can set them inline before the command, like PORT=3000 node app.js on Unix systems, or use a .env file with packages like dotenv.

Click to reveal answer
What type of value does process.env store for each environment variable?
AStrings
BNumbers
CBooleans
DObjects
How do you safely provide a default port if process.env.PORT is undefined?
Aconst port = process.env.PORT ? null : 3000;
Bconst port = 3000 || process.env.PORT;
Cconst port = process.env.PORT && 3000;
Dconst port = process.env.PORT || 3000;
Which package helps load environment variables from a .env file in Node.js?
Aexpress
Bdotenv
Cnodemon
Dcors
Why is it a bad idea to hardcode API keys directly in your Node.js code?
AIt makes the code run slower
BIt causes syntax errors
CIt exposes secrets if code is shared
DIt prevents the app from starting
What will console.log(process.env.MY_VAR) output if MY_VAR is not set?
Aundefined
Bnull
Cempty string
D0
Explain how to use process.env to manage configuration in a Node.js app.
Think about how environment variables help keep your app flexible and secure.
You got /4 concepts.
    Describe how to set environment variables locally and how to load them automatically in Node.js.
    Consider both manual and automated ways to provide environment variables.
    You got /3 concepts.