codewithmosh NodeJS course asks me a few times to change my NODE ENV - doesn t work on windows

0 votes

I'm currently learning "code with moshNodeJs "'s course, and I'm having trouble updating my NODE ENV environment variable... Every time I update it in CMD or Windows PowerShell, it appears to work, but nothing happens in the course's NodeJS Express Demo build. What I'm trying to figure out is how do you set up your ENV VARs.

The code is as follows:

const app = express();
const Joi = require('joi');
const helmet = require('helmet');
const morgan = require('morgan');
const config = require('config');
const dotenv = require('dotenv')
dotenv.config()

const logger = require('./logger');

console.log(`NODE_ENV: ${process.env.NODE_ENV}`);
console.log(`app ${app.get('env')}`);

app.use(express.json());
app.use(express.urlencoded({ extended: true }))
app.use(express.static('public'));
app.use(helmet());

if (app.get('env') === 'development') {
    console.log('morgan is Enabled');
    app.use(morgan('tiny'));
}```
Jun 14 in Node-js by Vaani
• 7,020 points
14 views

1 answer to this question.

0 votes

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));
answered Jun 14 by Neha
• 8,560 points

Related Questions In Node-js

0 votes
1 answer

How to create a directory if it doesn't exist using Node.js?

Hello @kartik, Try: var fs = require('fs'); var dir = ...READ MORE

answered Jul 9, 2020 in Node-js by Niroj
• 82,720 points
4,621 views
0 votes
1 answer

How can I set NODE_ENV=production on Windows?

Hello @kartik, It would be ideal if you ...READ MORE

answered Jul 13, 2020 in Node-js by Niroj
• 82,720 points
16,906 views
0 votes
0 answers

How to install a private NPM module without my own registry?

I've taken some code and put it in ...READ MORE

Jul 13, 2020 in Node-js by kartik
• 37,510 points
830 views
0 votes
1 answer

How to extract request http headers from a request using NodeJS connect?

Hello @kartik, To see a list of HTTP ...READ MORE

answered Jul 15, 2020 in Node-js by Niroj
• 82,720 points
13,566 views
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,790 points
967 views
0 votes
1 answer
0 votes
1 answer

Protocols used in a distributed/dlt system for the nodes to establish communication

yes all are over TCP/IP connections secured by TLS encryption in hashgraph architecture-hashgraph, ...READ MORE

answered Aug 6, 2018 in Blockchain by aryya
• 7,440 points
622 views
0 votes
1 answer
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP