Create-react-app: Is it possible to have .env out of the project folder?

Created on 26 Aug 2020  路  3Comments  路  Source: facebook/create-react-app

I have multiple projects within a single repo and I would like to have single .env file in the very root. My other projects use dotenv npm package with path setting. Is there any option to support such approach for create-react-app project?

stale

Most helpful comment

Hi @dhilt

good question, there was a similar issue raised about this in 2017 and I'm not sure whether any support for this has been planned since then: https://github.com/facebook/create-react-app/issues/1697

However, you could eject and configure the path like you mentioned:

require('dotenv').config({ path: '/full/custom/path/to/your/env/vars' })

If you don't want to eject, you could always create a little Bash script that loads in your central .env file and exports all environment variables into your shell before starting your React application. Or even easier, use dotenv-cli to do it for you:

"scripts": {
+    "build": "dotenv -e ../.env react-scripts build",
-    "build": "react-scripts build",
}

Hope this helps :)

All 3 comments

Hi @dhilt

good question, there was a similar issue raised about this in 2017 and I'm not sure whether any support for this has been planned since then: https://github.com/facebook/create-react-app/issues/1697

However, you could eject and configure the path like you mentioned:

require('dotenv').config({ path: '/full/custom/path/to/your/env/vars' })

If you don't want to eject, you could always create a little Bash script that loads in your central .env file and exports all environment variables into your shell before starting your React application. Or even easier, use dotenv-cli to do it for you:

"scripts": {
+    "build": "dotenv -e ../.env react-scripts build",
-    "build": "react-scripts build",
}

Hope this helps :)

This issue has been automatically marked as stale because it has not had any recent activity. It will be closed in 5 days if no further activity occurs.

This issue has been automatically closed because it has not had any recent activity. If you have a question or comment, please open a new issue.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

adrice727 picture adrice727  路  3Comments

Evan-GK picture Evan-GK  路  3Comments

Aranir picture Aranir  路  3Comments

xgqfrms-GitHub picture xgqfrms-GitHub  路  3Comments

wereHamster picture wereHamster  路  3Comments