---
title: "Fix NEXT_PUBLIC_SANITY_DATASET Error in Sanity CLI"
slug: "fix-missing-next-public-sanity-dataset-sanity-cli"
published: "2025-11-22"
updated: "2025-11-12"
validated: "2025-11-12"
categories:
  - "Sanity"
tags:
  - "NEXT_PUBLIC_SANITY_DATASET error"
  - "Sanity CLI"
  - "npx sanity dev"
  - "dotenv/config"
  - "sanity.cli.ts"
  - "env.ts fallbacks"
  - "Next.js environment variables"
  - "client-side env variables"
  - "Sanity Studio"
  - "projectId dataset"
  - "TypeScript"
llm-intent: "reference"
audience-level: "beginner"
framework-versions:
  - "next.js@15"
  - "sanity@3"
  - "typescript@5"
  - "node@18"
status: "stable"
llm-purpose: "NEXT_PUBLIC_SANITY_DATASET missing? Load dotenv in sanity.cli.ts and add env.ts fallbacks to stop the error when running npx sanity dev. Follow this…"
llm-prereqs:
  - "Access to Sanity CLI"
  - "Access to Next.js"
  - "Access to dotenv"
  - "Access to TypeScript"
  - "Access to Sanity Studio"
llm-outputs:
  - "Completed outcome: NEXT_PUBLIC_SANITY_DATASET missing? Load dotenv in sanity.cli.ts and add env.ts fallbacks to stop the error when running npx sanity dev. Follow this…"
---

**Summary Triples**
- (npx sanity dev, does not auto-load, .env file (unlike Next.js))
- (sanity.cli.ts, should import, 'dotenv/config' at the top to load .env before CLI initialization)
- (process.env.NEXT_PUBLIC_SANITY_PROJECT_ID and NEXT_PUBLIC_SANITY_DATASET, are used by, defineCliConfig({ api: { projectId, dataset } }) in sanity.cli.ts)
- (Browser/client-side code, cannot read, .env files directly; env.ts must provide safe fallbacks)
- (src/lib/sanity/env.ts, should avoid throwing on missing vars, use sensible defaults or conditional checks instead of assertValue)
- (import 'dotenv/config', ensures, environment variables in .env are available to Sanity CLI at runtime)

### {GOAL}
NEXT_PUBLIC_SANITY_DATASET missing? Load dotenv in sanity.cli.ts and add env.ts fallbacks to stop the error when running npx sanity dev. Follow this…

### {PREREQS}
- Access to Sanity CLI
- Access to Next.js
- Access to dotenv
- Access to TypeScript
- Access to Sanity Studio

### {STEPS}
1. Load dotenv in sanity.cli.ts
2. Add fallbacks in src/lib/sanity/env.ts
3. Restart and verify npx sanity dev

<!-- llm:goal="NEXT_PUBLIC_SANITY_DATASET missing? Load dotenv in sanity.cli.ts and add env.ts fallbacks to stop the error when running npx sanity dev. Follow this…" -->
<!-- llm:prereq="Access to Sanity CLI" -->
<!-- llm:prereq="Access to Next.js" -->
<!-- llm:prereq="Access to dotenv" -->
<!-- llm:prereq="Access to TypeScript" -->
<!-- llm:prereq="Access to Sanity Studio" -->
<!-- llm:output="Completed outcome: NEXT_PUBLIC_SANITY_DATASET missing? Load dotenv in sanity.cli.ts and add env.ts fallbacks to stop the error when running npx sanity dev. Follow this…" -->

# Fix NEXT_PUBLIC_SANITY_DATASET Error in Sanity CLI
> NEXT_PUBLIC_SANITY_DATASET missing? Load dotenv in sanity.cli.ts and add env.ts fallbacks to stop the error when running npx sanity dev. Follow this…
Matija Žiberna · 2025-11-22

I was setting up Sanity Studio for a project when I ran `npx sanity dev` and got hit with an error: "Missing environment variable: NEXT_PUBLIC_SANITY_DATASET". The frustrating part? The variable was clearly defined in my `.env` file. After digging into it, I realized the issue happens in two places. Here's exactly how to fix it.

## The Root Cause

When you run `npx sanity dev`, the Sanity CLI doesn't automatically load your `.env` file like Next.js does. Additionally, client-side components that import from your `env.ts` file can't access environment variables the same way server-side code can. The browser can't read `.env` files directly.

## Fix #1: Load dotenv in sanity.cli.ts

Your Sanity CLI configuration file needs to explicitly load environment variables. Open `sanity.cli.ts` and add the dotenv import at the top:

```typescript
// File: sanity.cli.ts
import { defineCliConfig } from 'sanity/cli'
import 'dotenv/config'

const projectId = process.env.NEXT_PUBLIC_SANITY_PROJECT_ID
const dataset = process.env.NEXT_PUBLIC_SANITY_DATASET

export default defineCliConfig({ api: { projectId, dataset }, studioHost: "your-studio-host" })
```

The `import 'dotenv/config'` line tells Node.js to load your `.env` file before anything else runs. This ensures the environment variables are available when the CLI initializes.

## Fix #2: Use Fallback Values in env.ts

The second issue is in your `src/lib/sanity/env.ts` file. If client-side components import from this file and the environment variables aren't available in the browser context, you'll get an error. Instead of throwing an error with `assertValue()`, provide sensible defaults:

```typescript
// File: src/lib/sanity/env.ts
export const apiVersion =
  process.env.NEXT_PUBLIC_SANITY_API_VERSION || '2024-12-27'

export const dataset = process.env.NEXT_PUBLIC_SANITY_DATASET || 'production'

export const projectId = process.env.NEXT_PUBLIC_SANITY_PROJECT_ID || 'ytvlzq9e'
```

This way, if the environment variables are available (injected by Next.js at build time), they get used. If not, the fallback values keep your client-side components working without throwing errors.

## Why Both Fixes Matter

The CLI fix ensures the Sanity CLI commands have access to your project configuration. The fallback values ensure that client-side code (like custom Sanity Studio components) doesn't crash when environment variables aren't directly accessible in the browser.

Now when you run `npx sanity dev`, it'll load your environment variables correctly, and your client-side components will work without errors.

Let me know in the comments if you hit any other environment variable issues with Sanity, and subscribe for more practical development guides.

Thanks, Matija

## LLM Response Snippet
```json
{
  "goal": "NEXT_PUBLIC_SANITY_DATASET missing? Load dotenv in sanity.cli.ts and add env.ts fallbacks to stop the error when running npx sanity dev. Follow this…",
  "responses": [
    {
      "question": "What does the article \"Fix NEXT_PUBLIC_SANITY_DATASET Error in Sanity CLI\" cover?",
      "answer": "NEXT_PUBLIC_SANITY_DATASET missing? Load dotenv in sanity.cli.ts and add env.ts fallbacks to stop the error when running npx sanity dev. Follow this…"
    }
  ]
}
```