node dotenv files not loading for test env

custom-env also solves this problem, it allows multiple configurations file for different environments. npm install custom-env. You can also specify which .env file to use on the go. require('custom-env').env('test');.

Full Docs here: https://www.npmjs.com/package/custom-env


Should I have multiple .env files?

No. We strongly recommend against having a "main" .env file and an "environment" .env file like .env.test. Your config should vary between deploys, and you should not be sharing values between environments.

From dotenv documentation


Please take a look at the dotenv-flow package.

This module extends dotenv adding the ability to have multiple .env* files like .env.development, .env.test, .env.production, etc., also allowing defined variables to be overwritten individually in the appropriate .env*.local file that is untracked by VCS.

Regarding to the recommendation against having multiple env files, dotenv-flow has a slightly different approach to manage .env* files under version control. Please refer the Files under version control section to understand the motivation of this approach.