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, 2022 in Node-js by Vaani
• 7,020 points
297 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, 2022 by Neha
• 9,060 points

Related Questions In Node-js

0 votes
0 answers

How to install NodeJS LTS on Windows as a local user (without admin rights)

I'm using Windows as a simple user ...READ MORE

Aug 11, 2022 in Node-js by Neha
• 9,060 points
1,411 views
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,880 points
5,734 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,880 points
29,232 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
1,372 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
1,705 views
0 votes
1 answer

Hyperledger Sawtooth vs Quorum in concurrency and speed Ask

Summary: Both should provide similar reliability of ...READ MORE

answered Sep 26, 2018 in IoT (Internet of Things) by Upasana
• 8,620 points
1,237 views
+1 vote
1 answer

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

yes all are over TCP/IP connections secured ...READ MORE

answered Aug 6, 2018 in Blockchain by aryya
• 7,450 points
1,148 views
0 votes
1 answer

How to host MEAN stack application with Angular and nodejs on windows IIS

It's fine that you're using Angular. Be ...READ MORE

answered May 27, 2022 in Node-js by Neha
• 9,060 points
1,037 views
0 votes
1 answer

How to schedule a google meet and get the meet link in NodeJs?

To create a Google Meet, you'll need ...READ MORE

answered May 27, 2022 in Node-js by Neha
• 9,060 points
3,463 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP