This article is useful when you’re almost ready to push an app live to production.
Consider the following workflow. You’ve already implemented most of the frontend and backend of your app, and it’s ready to go live in to production. All the while during development, you’re using the local Mongodb. You push it live onto Vercel, changing the environment variables for the DB to point to the production DB instead of the test DB. You then deploy it.
Suddenly, you realize that the DB hasn’t been right. The data isn’t showing up. You realize that, within your backend routes, you’ve hard-coded the MongoDB URI as process.env.MONGODB_URI, which is itself pointing to the live prod DB.
What if you need to do tests on the localDB, whilst also connecting to the production db without switching tediously? Well, here’s the trick.
In your package.json, add the node-env variable:
| |
This will ensure that when you do yarn run dev, the NODE_ENV variable will be set to development.
We can then use this as a flag to run our app in different modes.
| |
And then, in .env file, we’ll have the URI of both databases.
In my case, I’ll need to edit the URI in my mongo connectors.
That’s about it.