Search code examples
node.jsenvironment-variablespackage.jsonstrapi

Changing .env variables does not work on my Strapi(Node) application


In my .env file I have setup variables according to different environments. One example is simple Redis connection details. I had 3 environments setup: Development(default), Local and Production. Let's say the below are the credentials.

# Redis (DEV)
REDIS_HOST=127.0.0.1
REDIS_PORT=6378
REDIS_USERNAME=default
REDIS_PASSWORD=
# Redis (LOC)
LOC_REDIS_HOST=127.0.0.1
LOC_REDIS_PORT=6379
LOC_REDIS_USERNAME=default
LOC_REDIS_PASSWORD=
# Redis (PROD)
PROD_REDIS_HOST=127.0.0.1
PROD_REDIS_PORT=6380
PROD_REDIS_USERNAME=default
PROD_REDIS_PASSWORD=

In my package.json, I also have the following commands:

...
"build:dev": "yarn format && cross-env NODE_ENV=development strapi build --minify",
"build:prod": "yarn format && cross-env NODE_ENV=production strapi build --minify",
"build:loc": "yarn format && cross-env NODE_ENV=local strapi build --minify",
...

I have been successfully applying the correct environment variables for the past 3 months up until 2 days ago.

Mistakenly, I changed my mind thinking it will be a very quick fix. I want to change the LOC environment to now operate as DEV environment and create a new environment STAGE which will replace LOC. This will make more sense with the environments our team are working with.

# Redis Development (DEV - used for loc)
REDIS_HOST=127.0.0.1
REDIS_PORT=6378
REDIS_USERNAME=default
REDIS_PASSWORD=
# Redis Testing (STAGE)
STAGE_REDIS_HOST=127.0.0.1
STAGE_REDIS_PORT=6379
STAGE_REDIS_USERNAME=default
STAGE_REDIS_PASSWORD=
# Redis Live (PRODUCTION)
PROD_REDIS_HOST=127.0.0.1
PROD_REDIS_PORT=6380
PROD_REDIS_USERNAME=default
PROD_REDIS_PASSWORD=

This means I also made some changes to the scripts:

...
"build:dev": "yarn format && cross-env NODE_ENV=development strapi build --minify",
"build:prod": "yarn format && cross-env NODE_ENV=production strapi build --minify",
"build:stage": "yarn format && cross-env NODE_ENV=stage strapi build --minify",
...

But I cannot, for the life of me, make it work with the new environments. Whenever I run yarn build:stage it is always getting the DEV credentials, even tho it in my bash/CMD it states that my NODE_ENV is stage.

Removed node_modules, re-installed, cleared yarn and npm cache. Are they cached somewhere by any chance? Anyone ever had this happen?

Thanks!


Solution

  • According to Strapi's document, configurations are done by same variable name in different .env files:

    ./config/env/{environment}/{filename}
    

    and can customize the path of .env file by:

    ENV_PATH=/absolute/path/to/.env npm run start
    

    please refer to : https://docs.strapi.io/dev-docs/configurations/environment

    If your setup was functional before with different prefix variable name in one .env file, maybe you did some customization before. You could do a text search for NODE_ENV in /config to see if any clue. besides that, you can delete /dist or /cache in your project folder to make a clean build of Strapi.