How do I set environment variables in Node.js?

Question

Grade: Education Subject: Support
How do I set environment variables in Node.js?
Asked by:
46 Viewed 46 Answers

Answer (46)

Best Answer
(401)
You can set environment variables in Node.js in several ways. For temporary settings within a single terminal session, you can use shell commands like `export MY_VARIABLE=my_value` (Linux/macOS) or `set MY_VARIABLE=my_value` (Windows). For more persistent or project-specific settings, you can use a `.env` file and a library like `dotenv` to load them into `process.env` when your application starts.