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 in Node-js by Vaani
• 7,020 points
296 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 by Neha
• 8,740 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

1 day ago in Node-js by Neha
• 8,740 points
5 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,740 points
804 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,740 points
2,916 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,740 points
1,485 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,003 views
0 votes
1 answer
0 votes
1 answer

sudo: npm: command not found

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

answered Feb 17 in Others by Aditya
• 7,300 points
862 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 in Node-js by Neha
• 8,740 points
115 views
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