Fix: Config File Not Found in Astro

Error message:
Unable to resolve config file.
Configuration 2025-01-25

What Causes This Error?

This error occurs when Astro can’t find or load your configuration file. This typically happens when the config file is missing, misnamed, or in the wrong location.

The Problem

# Missing or misnamed config file
my-project/
├── src/
│   └── pages/
├── package.json
└── astro.config.js    ❌ Wrong extension (should be .mjs)

The Fix

Create Config File

// astro.config.mjs (in project root)
import { defineConfig } from 'astro/config';

export default defineConfig({
  // Your configuration
});

Common Scenarios

Correct File Name

# Valid config file names
astro.config.mjs    ✅ Recommended (ES modules)
astro.config.js     ✅ Works with "type": "module" in package.json
astro.config.ts     ✅ TypeScript config
astro.config.cjs    ✅ CommonJS format

# Invalid names
Astro.config.mjs    ❌ Case matters
astro-config.mjs    ❌ Wrong format
config.astro.mjs    ❌ Wrong format
.astro.config.mjs   ❌ Hidden file

Correct Location

# Config must be in project root
my-project/
├── astro.config.mjs    ✅ Correct location
├── src/
│   ├── astro.config.mjs   ❌ Wrong - not in root
│   └── pages/
└── package.json

# The config file should be next to package.json

Minimal Config

// astro.config.mjs
import { defineConfig } from 'astro/config';

// Minimal valid config (empty object)
export default defineConfig({});

TypeScript Config

// astro.config.ts
import { defineConfig } from 'astro/config';

export default defineConfig({
  site: 'https://example.com',
  output: 'static',
});

CommonJS Config

// astro.config.cjs
const { defineConfig } = require('astro/config');

module.exports = defineConfig({
  // Configuration
});

With Package.json Type Module

// package.json
{
  "type": "module"
}
// astro.config.js (works with "type": "module")
import { defineConfig } from 'astro/config';

export default defineConfig({});

Custom Config Path

# Specify config location explicitly
astro dev --config ./config/astro.config.mjs
astro build --config ./config/astro.config.mjs

Check for Syntax Errors

// ❌ Syntax errors prevent loading
export default defineConfig({
  site: 'https://example.com'  // Missing comma
  output: 'static'
});

// ✅ Valid syntax
export default defineConfig({
  site: 'https://example.com',
  output: 'static',
});

Missing defineConfig Import

// ❌ Missing import
export default {
  site: 'https://example.com',
};

// ✅ Import defineConfig
import { defineConfig } from 'astro/config';

export default defineConfig({
  site: 'https://example.com',
});

Create New Project

# Start fresh with proper config
npm create astro@latest my-project

# This creates a valid astro.config.mjs automatically

Debug Config Loading

# Check if config is found
npx astro info

# Verify Astro installation
npm ls astro

# Reinstall if needed
npm install astro@latest

Environment-Specific Config

// astro.config.mjs
import { defineConfig } from 'astro/config';

const isDev = import.meta.env.DEV;

export default defineConfig({
  site: isDev ? 'http://localhost:4321' : 'https://example.com',
});

Quick Checklist

  • Config file is in project root (next to package.json)
  • File is named astro.config.mjs (or .ts, .js, .cjs)
  • File has correct case (lowercase)
  • Import defineConfig from ‘astro/config’
  • Use export default (ES modules) or module.exports (CommonJS)
  • Check for syntax errors