npm dotenv environment variables not recognized in nodejs module

0 votes

Here is my project structure:

image

package.json:

  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "dev": "nodemon $NODE_DEBUG_OPTION server/boot.js --exec babel-node",
     "start": "nodemon server/boot.js --exec babel-node",
     "build": "babel server -d dist/server",
     "serve": "node dist/server/boot.js"
   },

The main file is server/boot.js:

import dotenv from 'dotenv';
import path from 'path';

dotenv.load({path: path.join(__dirname, '.env')});

import _ from 'underscore';
import configs from './config/index';

The server/config/index.js is only a barrel file that imports the other config files:

import app from './app';
import database from './database';

export default Object.assign({}, app, database);

In each of the config files I am not able to access any properties of the process.env object that are defined in the .env file.

Here is one of the config files for reference:

export default  {
    app: {
        host: process.env.HOST || 'localhost',
        port: process.env.PORT || 9000,
    }
}

Here process.env.HOST is undefined, but the key is present in the .env file.

What is wrong with my code?

Jun 10, 2022 in Node-js by Vaani
• 7,070 points
9,406 views

1 answer to this question.

0 votes

process.env object that are defined in the .env file.

Can you please be more specific about the process.env file?

As per https://www.npmjs.com/package/dotenv#rules the file should be in the format:

VAR1=value1
VAR2=value2

and not in

export default { 
    VAR1: 'value1', 
    VAR2: 'value2' 
}
answered Jun 10, 2022 by Neha
• 9,020 points

Related Questions In Node-js

0 votes
0 answers

npm command not working after installing nodejs in windows10?

i have installed nodejs from nodejs website. ...READ MORE

Aug 11, 2022 in Node-js by Neha
• 9,020 points
1,574 views
0 votes
1 answer

How to read environment variables in Node.js?

Hello @kartik, Yes,you can read environment variables in Node.js ...READ MORE

answered Jul 8, 2020 in Node-js by Niroj
• 82,840 points
1,634 views
0 votes
1 answer

Error:npm WARN unmet dependency in nodejs

Hii @kartik, Following are the possible solution : Manually ...READ MORE

answered Jul 12, 2020 in Node-js by Niroj
• 82,840 points
6,336 views
0 votes
1 answer

Error:Failed to parse json-This is not a bug in npm.

Hello @kartik, Try this: npm cache clean Hope it works!! Thank ...READ MORE

answered Nov 27, 2020 in Node-js by Niroj
• 82,840 points
4,803 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,947 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,472 views
0 votes
1 answer

sudo: npm: command not found

To answer your question, for me, the ...READ MORE

answered Feb 17, 2022 in Others by Aditya
• 7,680 points
5,136 views
0 votes
1 answer

Nodejs: question of type input with npm inquirer not working

The right code should be: const outPath = ...READ MORE

answered Jun 7, 2022 in Node-js by Neha
• 9,020 points
1,908 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,020 points
3,909 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