The solution you require is straightforward.
You must first install Dotenv. "npm install dotenv" can be used to accomplish this.
Second, in the root directory of your project, create a file called ".env" (same directory as your package.json file)
Third, add the following line to the.env file (without the quotes): "NODE ENV = development"
There are no semi-colons in this sentence.
Here's an example of how I utilise it in one of my projects. Note that in the newest version of Node, I have modules enabled, so my import statements may differ from what you're used to
import express from "express";
import colors from "colors";
import dotenv from "dotenv";
import morgan from "morgan";
import connectDB from "./config/db.js";
import { notFound, errorHandler } from "./middleware/errorMiddleware.js";
dotenv.config();
// Connect Database
connectDB();
const app = express();
// Middleware
// Only run Morgan in development mode
if (process.env.NODE_ENV === "development") {
app.use(morgan("dev"));
}
app.use(express.json());
// Routes
app.get("/", (req, res) => {
res.send("API is online...");
});
// Error and 404 handling Middleware
app.use(notFound);
app.use(errorHandler);
// Start server
const PORT = process.env.PORT || 5000;
app.listen(PORT, console.log(`Server is running on port ${PORT}`.green.bold));