How to use Environment Variables in Packages In NodeJS
Some packages, such as express
, mongoose
, and sequelize
, allow you to configure settings using environment variables directly:
const express = require('express');
const app = express();
const port = process.env.PORT || 3000;
app.listen(port, () => {
console.log(`Server listening on port ${port}`);
});
How to handle environment variables in Node ?
Environment variables are dynamic values that can affect the behavior of NodeJS applications across different environments such as development, staging, and production. Managing environment variables properly is essential for configuring applications, connecting to databases, and controlling application behavior. Here’s how you can handle environment variables in NodeJS effectively: