Skip Navigation

Fix import.meta.env Unexpected token error in Vite

If you use Vite with environment variables which sometimes are undefined depending on the build and environment, you might be getting an error like this:

error during build:
Error: Unexpected token (Note that you need plugins to import files that are not JavaScript)

And the cause is an environment variable that is not defined.

The code that triggered such error in my case was:

const useEmulator = () => import.meta.env.VITE_FIREBASE_EMULATOR as string

To fix an error like this, change the above code into:

const useEmulator = (): boolean => {
  return import.meta.env.VITE_FIREBASE_EMULATOR !== undefined && import.meta.env.VITE_FIREBASE_EMULATOR === 'true';

Happy coding!