How to use .env in custom controller

System Information
  • Strapi Version: 3.4.6
  • Operating System: Window 10
  • Database: MySQL
  • Node Version: 14.15.5
  • NPM Version: 6.14.11

Hello everyone,
I already read doc about env in this link
Configurations - Strapi Developer Documentation
but I couldn’t figure it out how to use env in controller file.
In my code, I tried like this but it didn’t work.

module.exports = ({ env }) => ({
  async test(ctx) {
    const SOME_ENV = env('SOME_ENV');
    ctx.send(SOME_ENV);
  }
});


Thank for any help

1 Like

Example:

const credentials = {
email: process.env.AWS_LOGIN_EMAIL,
password: process.env.AWS_LOGIN_PASSWORD,
};

In your .env-File (or Environment):

AWS_LOGIN_EMAIL=test
AWS_LOGIN_PASSWORD=123

5 Likes

Hi @thitiphan

As said @lolafkok, you can’t use the env() function in a controller. It only works for configurations file :wink:
You have to use the process.env object in order to retrieve your environment variable which are in the .envfile

2 Likes

@lolafkok @Stun3R thank you for your solution and explanation :smile:

1 Like

Thank you!

thank you. :smiley: :smiley: :smiley: