I have 3 different databases set up for testing, development and production.
Now I would like my express app to switch between these databases depending on which script I run.
These are my current scripts:
"scripts": {
"start": "NODE_ENV=dev && node index.js",
"test": "mocha --timeout 10000"
}
I am assigning the process.env.NODE_ENV value at the top of the test script like this, and the test script runs fine against the correct DB:
process.env.NODE_ENV = 'test';
But when I run npm start, node does not seem to get the value. For instance on this code:
// DB connection
let db;
if (process.env.NODE_ENV == 'test') {
mongoose.connect(config.DB.test, options);
db = mongoose.connection;
} else if (process.env.NODE_ENV == 'dev') {
mongoose.connect(config.DB.dev, options);
db = mongoose.connection;
app.use(morgan('combined'));
} else if (process.env.NODE_ENV == 'prod') {
mongoose.connect(config.DB.prod, options);
db = mongoose.connection;
app.use(morgan('combined'));
}
db.on('error', console.error.bind(console, 'connection error:'));
I get the Cannot read property 'on' of undefined error (while running the same code with the test script connects to the correct DB).
What am I doing wrong?