@kirz/expo-env
Version:
Simple Expo env loader with TypeScript and schema validation
72 lines (59 loc) • 2.19 kB
Markdown
`@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:
```bash
npm install @kirz/expo-env
yarn add @kirz/expo-env
pnpm add @kirz/expo-env
```
This package requires [Zod](https://www.npmjs.com/package/zod) as a peer dependency for schema validation. You can install it as follows:
```bash
npm install zod
yarn add zod
pnpm add zod
```
Add the following script to your package.json to automatically load environment variables before starting your Expo project:
```json
"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](https://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:
```typescript
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:
```typescript
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:
```typescript
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,
};
};
```