Sharing an environment file across your Mono-Repo

The Executioner
1 min readFeb 2, 2020

--

It can be useful to have a centralized config containing all your environment variables to avoid code duplication and improve re-usability. Rather than creating a .env file per package we can just use one .env file located at the root of the project.

We can then use this helper method to locate the file path of the .env file.

npm i find-up . Find-up works by walking up the parent directories.

import find from 'find-up';export const findEnv = () => find.sync(process.env.ENV_FILE || '.env');

Then we can use this findEnv like so:

import { findEnv } from './find-env';dotenv.config({ path: findEnv() });console.log(process.env.MY_ENV);

--

--