Accessing environment variables

I’ve created an .env file with my vars, and am calling process.env.APP_API_KEY from the _init function of one of my components, but I’m getting the error: ‘process’ is not defined.

hi, can you maybe share your .env file (redacting private info, off course :wink: ) and a code snippet of where you try to use the environment variable in your code?

That will make it easier for us to help you out.

Hi Michiel, sure thank you!

.env

NODE_ENV=production
APP_API_KEY=mysecretapikey

Login.js

export default class Login extends Lightning.Component {
  static _template() {
    return {
      TextTexture: {
        x: 50,
        y: 400,
        colorTop: 0xfff1f1f1,
        colorBottom: 0xff144b83,
        text: { text: 'TEXT', fontSize: 40, wordWrap: true, wordWrapWidth: 450, lineHeight: 30 },
        Desc: {
          x: 0,
          y: 50,
          text: {
            text: 'Login page',
            fontSize: 22,
            wordWrap: true,
            wordWrapWidth: 450,
            lineHeight: 30,
          },
        },
      },
    }
  }

  _init() {
    console.log(process.env.APP_API_KEY)
  }
}

I am also using Router in the base of my app, if that makes a difference.

Are you running the app with the Lightning CLI - aka lng dev. I’ve just tested this and its working for me. Process.env is created via the build tool.

If you’re not using the lng cli you need to add dotenv

I am using lng dev to start the server. I will try creating a new project and see if it works there.

Make sure you have the latest lng cli installed as well.

It was a linter problem. I updated the globals object in eslintrc.js to include process, and now it works. Thanks for your help!