@kirz/expo-env
is a simple environment loader designed for Expo projects, offering TypeScript support and schema validation using Zod. This package helps ensure your environment variables are correctly typed and validated, providing an easy way to manage them within your Expo app.
You can install the package using your preferred package manager:
npm install @kirz/expo-env
# or
yarn add @kirz/expo-env
# or
pnpm add @kirz/expo-env
This package requires Zod as a peer dependency for schema validation. You can install it as follows:
npm install zod
# or
yarn add zod
# or
pnpm add zod
Add the following script to your package.json to automatically load environment variables before starting your Expo project:
"scripts": {
"prestart": "load-env"
}
To significantly enhance script execution speed, we’ve adopted the modern Bun JavaScript runtime. This change has led to a roughly 50% reduction in the time required to run scripts. To take advantage of this performance boost, please install Bun.sh.
In the root of your project, create a file named env.ts. This file will define your environment variables using Zod for schema validation. Here’s an example:
import z from "zod";
export default {
APP_NAME: z.string(),
APP_BUNDLE_ID: z.string(),
APP_VERSION: z.string().default("1.0"),
};
In any file where you need to access your environment variables, import the Env object like this:
import { Env } from "@kirz/expo-env";
You can now access your validated environment variables anywhere in your project, including in app.config.ts. For example:
import { Env } from "@kirz/expo-env";
import { ConfigContext, ExpoConfig } from "@expo/config";
export default ({ config }: ConfigContext): ExpoConfig => {
return {
...config,
name: Env.APP_NAME,
slug: Env.APP_BUNDLE_ID,
version: Env.APP_VERSION,
};
};