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,020 points
8,582 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,060 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,060 points
1,161 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,880 points
1,360 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,880 points
5,633 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,880 points
3,945 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,663 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,215 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
4,556 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,060 points
1,485 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,391 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