2022-07-12 19:00:04 +01:00
---
2022-09-06 15:44:40 +01:00
categories:
- Programming Languages
2022-07-12 19:00:04 +01:00
tags:
- backend
- node-js
---
2022-09-06 15:44:40 +01:00
# Managing environments
2022-07-12 19:00:04 +01:00
With a full-scale Node application you will typically run three environments:
2022-09-06 15:44:40 +01:00
- Development
- Testing
- Production
2022-07-12 19:00:04 +01:00
## Accessing the current environment
2024-02-02 15:58:13 +00:00
To determine the current environment we can use the variable
**`process.env.NODE_ENV` ** from the
[global process object ](/Programming_Languages/Node/Architecture/Process_object.md ).
This works universally regardless of the kind of Node app we are building.
2022-07-12 19:00:04 +01:00
2024-02-02 15:58:13 +00:00
If you have not manually set up your environments, ** `process.env.NODE_ENV` **
will return `undefined` .
2022-07-12 19:00:04 +01:00
### Setting the Node environment
2022-09-06 15:44:40 +01:00
2022-07-12 19:00:04 +01:00
#### For the session
2022-09-06 15:44:40 +01:00
2024-02-02 15:58:13 +00:00
`NODE_ENV` is a bash
[environment variable ](/Programming_Languages/Shell_Scripting/Environmental_and_shell_variables.md )
like any other. So we can set it in the normal way:
2022-07-12 19:00:04 +01:00
```bash
export NODE_ENV=production
```
### In Express
2022-09-06 15:44:40 +01:00
2024-02-02 15:58:13 +00:00
In Express, there is a built in method for retrieving the current envrionment:
`app.get('env')` . Express will default to the development environment.
2022-07-12 19:00:04 +01:00
< p style = "color:red" > ! How to keep Express and Node environment in sync?< / p >
2022-09-06 15:44:40 +01:00
## Configuring environments
2022-07-12 19:00:04 +01:00
2024-02-02 15:58:13 +00:00
We use the third-party [Config ](https://github.com/node-config/node-config )
package to manage different configurations based on the environment.
2022-07-12 19:00:04 +01:00
2024-02-02 15:58:13 +00:00
Once installed we set up a dedicated config directory with a structure as
follows:
2022-07-12 19:00:04 +01:00
```
config/
default.json
development.json
production.json
```
2022-09-06 15:44:40 +01:00
For example:
2022-07-12 19:00:04 +01:00
```json
2022-09-06 15:44:40 +01:00
// default.json
2022-07-14 08:00:04 +01:00
{
2022-09-06 15:44:40 +01:00
"name": "My Express app"
2022-07-14 08:00:04 +01:00
}
2022-07-12 19:00:04 +01:00
```
2022-09-06 15:44:40 +01:00
2022-07-12 19:00:04 +01:00
Then to utilise config variables:
```js
2023-01-16 14:56:28 +00:00
const config = require("config");
console.log("Application name:" + config.get("name"));
2022-07-12 19:00:04 +01:00
```
2024-02-02 15:58:13 +00:00
If we toggled the different environments, we would see different outputs from
the above code (assuming we had different config files in `/config` with
different names).
2022-07-12 19:00:04 +01:00
### Sensitive config data
2024-02-02 15:58:13 +00:00
We will need to store passwords, API keys and other kinds of authentication data
for our application. We obviously shouldn't store this data openly in our config
files since it would be made public.
2022-07-12 19:00:04 +01:00
2024-02-02 15:58:13 +00:00
We can do so securely by utilising
[environmental variables ](../Shell_Scripting/Environmental_and_shell_variables.md )
alongside the config pacakage.
2022-07-12 19:00:04 +01:00
2024-02-02 15:58:13 +00:00
We create a file called `custom-environment-variables` (must be called this) and
map a property to an environmental environment we have already set.
2022-07-12 19:00:04 +01:00
Let's create an environmental variable for a password:
2022-09-06 15:44:40 +01:00
2022-07-12 19:00:04 +01:00
```bash
export APP_PASSWORD='mypassword123'
```
Then in our custom variable file:
```json
{
2022-09-06 15:44:40 +01:00
"password": "APP_PASSWORD"
2022-07-12 19:00:04 +01:00
}
```
We can then safely reference this value in the course of our normal code:
2022-09-06 15:44:40 +01:00
2022-07-12 19:00:04 +01:00
```js
2023-01-16 14:56:28 +00:00
console.log(config.get("password"));
2022-07-12 19:00:04 +01:00
```
2022-07-14 08:00:04 +01:00
< p style = "color:red" > ! But how would this be achieved in a production server?< / p >
2022-07-12 19:00:04 +01:00
2022-09-06 15:44:40 +01:00
< p style = "color:red" > ! And how could we do this programmatically at the start of a local development session without manually setting each environment variable in the terminal?< / p >