1

Environment: Node.js, dotenv

require('dotenv').config() is typically only required in the main file such as app.js. With that one call process.env can be referenced in every file in the app. This answer agrees with that, https://stackoverflow.com/a/58697426/12582054

However I just ran across an instance where I get an error if I don't include require('dotenv').config() in an additional file and I'm not sure why.

Simplified Example:

app.js

const path = require('path');

// Custom path to .env file.
require('dotenv').config({ path: path.join(__dirname, '/models/.env')});

const middlewareFile = require('./controllers/middleware-file');

middleware-file.js

const USPS = require('usps-webtools-promise').default;

const usps = new USPS({
    userId: process.env.USPS_USER_ID,
    properCase: Boolean
});

usps-webtools-promise appears to be the module triggering the error.

If I don't include the dotenv reference at the top of middleware-file.js the USPS module throws an error. It points exactly at the first letter of the value process.env.USPS_USER_ID.

throw new error_1.default("Must pass USPS userId");

USPSError [USPS Webtools Error]: Must pass USPS userId

at Object. (c:\website\controllers\middleware-file.js:35:14)

I can solve the error by adding dotenv to the top of the file.

const path = require('path');
require('dotenv').config({ path: path.join(__dirname, '../models/.env')});
myNewAccount
  • 578
  • 5
  • 17
  • Can you try `const dotenv = require('dotenv').config({ path: path.join(__dirname, '/models/.env')}); console.log({dotenv});` in app.js? – pzaenger Nov 08 '20 at 01:59
  • @pzaenger console came back, `ReferenceError: dotenv is not defined` – myNewAccount Nov 08 '20 at 02:03
  • That's not so good. Please check your package.json. Did you add `dotenv` to your dependencies? – pzaenger Nov 08 '20 at 02:05
  • `"dotenv": "^8.2.0"`, it works fine throughout the app which is several thousand lines. It is called a hundred times and except for this one instance works as expected. – myNewAccount Nov 08 '20 at 02:06
  • 1
    Do you call `require('dotenv').config(...)` more than once? Normally you would call it just once: [_As early as possible in your application, require and configure dotenv._](https://www.npmjs.com/package/dotenv) – pzaenger Nov 08 '20 at 02:08
  • 1
    That was it! I bumped it up to the top of app.js and it worked. Thanks so much! Maybe make an official answer so I can check it. – myNewAccount Nov 08 '20 at 02:10
  • Sounds good. Glad, I could help you :) – pzaenger Nov 08 '20 at 02:14

1 Answers1

1

Make sure to call require('dotenv').config({ path: path.join(__dirname, '../models/.env')}); as early as possible in your application. Further you can log the results or check for errors with:

const dotenv = require('dotenv').config({ path: path.join(__dirname, '/models/.env')});

if (dotenv.error) {
  // Something went wrong
  console.error(dotenv.error);
} else {
  // Log parsed values
  console.log(dotenv.parsed);
}
pzaenger
  • 11,381
  • 3
  • 45
  • 46