EAS Build Failing due to .env (react-native-dotenv, typescript, bare workflow)

Before posting your question, make sure you’ve read the documentation for both EAS Build & Submit.

If your Android or iOS build is failing- make sure you do your own research on the error message first before posting (this will give you a better understanding, and you might even find that someone else has already solved the issue!)

I’m using react-native-dotenv which adds a .env file to the root of my project. From reading the Xcode logs, it is clear that this file isn’t included in the build within EAS. Having read the docs on env variables, I tried implementing a constants.ts file which imports and exports my variables. I switched all imports from @exp to @utilities/constants. Before submitting a build via EAS again, I tried running the app locally via Xcode and I’m now getting this error which I can’t make sense of so I’m wondering if there is an additional step.

src/utilities/constants.ts: Property local of ExportSpecifier expected node to be of a type ["Identifier"] but instead got "StringLiteral"

Here is my constants.ts (with alternate values for the variables)

import {
    VAR_1,
    VAR_2,
    VAR_3
} from '@env'

export {
    VAR_1,
    VAR_2,
    VAR_3
}

Here is how the variables are being imported within my project:

import { VAR_1 } from '@utilities/constants'

Here is my babel.config.js:

module.exports = api => {
    api.cache(true)
    return {
        presets: ['babel-preset-expo'],
        plugins: [
            [
                'module:react-native-dotenv',
                {
                    moduleName: '@env',
                    path: '.env'
                }
            ],
            [
                'module-resolver',
                {
                    extensions: ['.png', '.svg', '.ts', '.tsx'],
                    root: ['./'],
                    alias: {
                        '@types': './src/types',
                        '@utilities': './src/utilities'
                    }
                }
            ]
        ]
    }
}

And here is my environment variable types file env.d.ts:

declare module '@env' {
    export const VAR_1: string
    export const VAR_2: string
    export const VAR_3: string
}

Even if I do get this to build, I’m concerned that this set still won’t work with EAS build because it is still reading from a .env rather than process.env.VAR_1, as an example. Any input would be greatly appreciated, thanks.

hi there,

i don’t totally understand everything that is being done here and most of it is unrelated to eas build, and rather it’s about module-resolver and react-native-dotenv.

the suggestion in Environment variables and secrets is to provide a fallback value if your environment variable is not available, it’s not opinionated at all about how you read from your env.

that said, very soon (maybe tomorrow) you will be able to pass in values in an env key in eas.json if you’d like to set env vars on your build. you can see the pr for the docs here: [docs] EAS Build configurable build environment by wkozyra95 · Pull Request #11845 · expo/expo · GitHub

1 Like

@notbrent Thanks for the reply. Given that that PR is just the docs, does that mean that I could already begin to utilize that functionality, it’s just not yet documented?

unfortunately not yet - here’s a pr that adds support for it to eas-cli: [eas-cli] configurable builder environment by wkozyra95 · Pull Request #230 · expo/eas-cli · GitHub - and the server component is only on staging

got it thanks, it seems you’ll still have to provide the actual values for these environment variables in eas.json so I have some work to do moving one token out of the client in the meantime. Will there eventually be an interface in the Expo dashboard for setting those values?