Reputation: 193
I am attempting to use the value for NODE_ENV in my node.js server to connect to a different database when running my tests. How can I get the correct values for NODE_ENV i.e 'development', 'test', 'production' to make what I've got below work?
import pg from "pg";
import dotenv from "dotenv";
dotenv.config();
const Pool = pg.Pool;
const enviroment = () => {
if (process.env.NODE_ENV === "test") {
return process.env.TEST_DATABASE;
} else {
return process.env.DEVELOPMENT_DATABASE;
}
};
const pool = new Pool({
password: process.env.PASSWORD,
host: process.env.HOST,
port: process.env.PORT,
database: enviroment()
});
export default pool;
.env includes
NODE_ENV=development
NODE_ENV=production
NODE_ENV=test
Thanks.
Upvotes: 0
Views: 392
Reputation: 2286
Seems your code is fine. I think you made a typo in your .env
file.
Here is a sample for your .env
:
NODE_ENV=test
TEST_DATABASE=mongodb://localhost:27017
DEVELOPMENT_DATABASE=mongodb://localhost:27018
PASSWORD=abcxyz
HOST=localhost
PORT=8000
Don't use NODE_ENV="test"
, spaces, or anything like this.
Upvotes: 1