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
31 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,740 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 in Node-js by Neha
• 8,740 points
12 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,740 points
4,771 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,740 points
18,111 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,500 points
868 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,009 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
650 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