---
id: 3038
title: Multiple Environments with Loopback 4
slug: multiple-environment-with-loopback-4
date: '2019-09-17T07:01:29Z'
modified: '2019-09-17T07:06:12Z'
status: publish
type: post
author: pmoelgaard
categories:
  - Documentation
  - Uncategorized
tags:
  - Loopback
excerpt: null
canonicalPath: /posts/multiple-environment-with-loopback-4
featuredImage: null
commentCount: 1
wordCount: 188
readingTimeMinutes: 1
hasMedia: false
hasVideos: false
url: 'https://forwardthinking.pemo.me/posts/multiple-environment-with-loopback-4'
---
When configuring multiple environments for a Loopback 4 Application, it can appear less straight forward than with Loopback 3, so here is a quick fix for how to get over this obstacle as elegantly as possible in 5 minutes.

1.  Import dotenv and create a .env file in the root of the app.
2.  Put your database configuration in this file.
3.  Empty the existing datasource file so it contains an empty JSON (file is required by compiler and the bootstrapping).
4.  Add the code below to the application.ts file in the src directory.
5.  Don't forget to require the dotenv from the run command.

> this.bind('datasources.default').to(new DefaultDataSource());
> this.dataSource(new DefaultDataSource());
> this.bind('datasources.config.default').to({
>   name: 'default',
>   connector: 'mongodb',
>   hostname: process.env.DB\_DEFAULT\_HOSTNAME,
>   port: process.env.DB\_DEFAULT\_PORT,
>   user: process.env.DB\_DEFAULT\_USER,
>   password: process.env.DB\_DEFAULT\_PASSWORD,
>   database: process.env.DB\_DEFAULT\_DATABASE,
>   useNewUrlParser: process.env.DB\_DEFAULT\_USENEWURLPARSER,
>   authSource: process.env.DB\_DEFAULT\_AUTHSOURCE
> });
> this.bind('datasources.default').toClass(DefaultDataSource);

This is a quick fix intended to help move on if in a pickle, I will refine this further so the application will check the process.env.NODE\_ENV variable instead and load the corresponding file. For the original inspiration to this post, head over here: [https://medium.com/p/8f085399268/responses/show](https://medium.com/p/8f085399268/responses/show)
