DEV Community

humanfriend22
humanfriend22

Posted on • Updated on

Load JSON into env in NodeJS

Almost all of us have heard of dotenv, which loads key-value pairs from .env into process.env so it is globally accessible.

But what if we don't want to install another package to perform a similar task? We can keep it as a one-liner to load a JSON file into process.env.

NodeJS has a built-in function called Object.assign which merges one object to another object. In our case, object 1 would process.env & object 2 would be our parsed JSON. We would then want to set process.env to our updated object.

EDIT: Object.assign automatically adds the values to the first object and doesn't create a new object (like Object.create) so we don't need the process.env = before the rest of the assignment.

Let's do exactly that:

Top comments (0)