Skip to content

Dotenv file custom path/env #11335

@Xerphis

Description

@Xerphis

Is your proposal related to a problem?

Standard support for environment files is limited to reading defined paths (https://github.com/bkeepers/dotenv#what-other-env-files-can-i-use); Therefore, it is impossible to define what environment file we want to use when building a project for non-end users build, but for example, staging/demo.

Describe the solution you'd like

Allow the definition of an additional pre-production environment by setting an env variable (eg. DOTENV_CUSTOM_ENV) or an argument for npm run buld --staging/demo. In the following order or otherwise:
react-scripts/config/env.js

// https://github.com/bkeepers/dotenv#what-other-env-files-can-i-use
const dotenvFiles = [
	`${paths.dotenv}.${NODE_ENV}.local`,
	// Don't include `.env.local` for `test` environment
	// since normally you expect tests to produce the same
	// results for everyone
	NODE_ENV !== "test" && `${paths.dotenv}.local`,
	`${paths.dotenv}.${process.env.DOTENV_CUSTOM_ENV}`,
	`${paths.dotenv}.${NODE_ENV}`,
	paths.dotenv,
].filter(Boolean);

With this solution, running a script by "DOTENB_CUSTOM_ENV=staging npm run build", react-scripts reads the file .env.staging

Describe alternatives you've considered

An alternative solution would be to allow set the full path to the .env file. This method would point to an environment file that could be used by many different projects.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions