Saturday, November 6, 2021

Using custom .env file for react project

By default, react project supports .env, .env.development, .env.production and .evn.test for different build and test scenario. However, sometimes, you may need additional testing and deployment configuration with custom env file. The following steps can be used to setup custom react env files:

1. run below command to add env-cmd package to your react project

npm install env-cmd

2. create your custom env file, for example, .evn.staging in your project in the same folder as other .env files. Then add the environment variables and values in the file in the same way as other .env file.

3. in the project's package.json file's script section, add "env-cmd -f YourCustomEnvFileName" parameter before other command parameter to indicate which .env file should be used for the command, for example:

 "start": "env-cmd -f .env.staging react-scripts start", 

will start the react app by using the configuration in evn.staging file.

No comments:

Post a Comment