Skip to content

Commit

Permalink
add new post about .env file with node 20.6.0
Browse files Browse the repository at this point in the history
  • Loading branch information
nejos97 committed Sep 17, 2023
1 parent 4a1f033 commit 24ce23d
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
86 changes: 86 additions & 0 deletions _posts/2023-09-17-node-now-supports-built-in-env-files.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
---
title: 'Node(20.6.0) now supports built-in .env files'
layout: post
date: '2023-09-17 2:00:00 AM'
categories:
- node
- env
- javascript
- programming
image: /assets/images/posts/node-now-supports-built-in-env-files.png
visible: 1
description: Learn how to manage .env with node 20.6.0 without using an external package.
---

Hello javascript lovers, for some time now the new version of node, more precisely version 20.6.0, has been supporting your environment variable file without the need for an external package to do this. Well, in this little post we're going to talk a bit about what this means and how to take full advantage of this improvement, as well as some of the drawbacks or weak points of this feature.

To begin with, environment variables are essential for our code, and their usefulness is well established. This allows us to avoid exposing sensitive data in our source code, and also to easily change certain configurations in our application without having to hardcode them.

In the development world, we're used to using a file to store our data. Often we call it `.env` `.env.local` `env.uat` `.env.prod`, depending on your environment.

In javascript, the essential package for managing configuration files was `dotenv`, which was used, and still is today, to load env files and perform other configuration. Since node version 20.6.0, you can do it without this package.

What's the advantage of using this

Firstly, this means we don't have to depend on an external library, and therefore limit security failures in our code.

more simplicity, so we won't need to install and configure the dotenv package in our project as we used to do.

So without further ado, let's get to work!

So I'm not going to tell you again to install node version 20.6.0 - I assume you've already done that - but for a little tip I recommend using nvm (node version manager), which lets you have several versions of node installed on your computer, so you can easily switch versions independently of the projects you're working on.

create a folder and initialize a project node with the command

```bash
npm init --yes
```

once this is done create two files `index.js` and `.env` at the root of your folder, place this content in the `.env` file

```
PORT=3000
APP_NAME=fake-app-name
LOG_LEVEL=debug
```

and this in the `index.js` file

```javascript
console.log(process.env.PORT)
console.log(process.env.APP_NAME)
console.log(process.env.LOG_LEVEL)
```

Now to launch your little application, type the following command in your terminal and you'll see the contents of your different environment variables displayed.

```bash
node --env-file=.env index.js
```

as you can see, we didn't have to install anything or configure anything to have our variables available in the code, now it's up to you to improve it and define the file by environment.

```json
{
"name": "test_env",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start:dev": "node --env-file=.env index.js",
"start:uat": "node --env-file=.env.uat index.js",
"start:prod": "node --env-file=.env.prod index.js"
},
"keywords": [],
"author": "",
"license": "ISC"
}
```

From the release of this announcement there have been some questions about this feature, for example:

It doesn't traverse parent directories, doesn't have good overwrite/merge logic - things that dotenv supported.

It doesn't support multiline variables, which are very important for keys, certificates and JSON configuration.

Above all, it should be noted that this functionality has been made public and that it's up to the community to decide how to improve it and make it more suited to our needs.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 24ce23d

Please sign in to comment.