We use cookies to ensure you get the best experience on our website. Read our privacy policy

Skip to main content
Bad Indentation Of A Mapping Entry illustration showing code and data structures
Home Blog Debug

Fix Bad Indentation Of A Mapping Entry Error In Astro

Troubleshoot and resolve the common "bad indentation of a mapping entry" error in AstroJS with this practical guide.

Published on:
Check Circle Difficulty: easy

The “bad indentation of a mapping entry” error in Astro occurs in your frontmatter section (the area between the triple dashes --- at the top of your .mdx files). This error has several potential causes:

YAML (the format used in frontmatter) is extremely sensitive to indentation. Each level of nesting must use consistent spacing:

---
# Incorrect indentation
layout: ../layouts/BlogPost.astro
title: My Blog Post
author: # This line has extra spaces
name: John Doe
---

The correct version would align the indentation properly:

---
# Correct indentation
layout: ../layouts/BlogPost.astro
title: My Blog Post
author: # Properly aligned
name: John Doe
---

One of the most common causes is mixing tabs and spaces in your indentation. YAML requires consistent use of either tabs or spaces (with spaces being the recommended approach):

---
layout: ../layouts/BlogPost.astro
title: My Blog Post
author:
name: John Doe # This line uses a tab instead of spaces
email: [email protected] # This line uses spaces
---

Unescaped special characters in your frontmatter strings can break the YAML parser:

---
title: 'My Page's Title' # The apostrophe breaks the string
---

Limited Time Launch Sale

Skip the debugging headaches! Our AstroJS starter kit includes examples of all the frontmatter syntax, saving you hours of troubleshooting time.

GET 60% OFF!

If your error involves strings with special characters like apostrophes or quotes, you have several options:

---
# Option 1: Use double quotes for strings with apostrophes
title: "My Page's Title"
# Option 2: Use YAML's multiline string format
description: >-
This is a long description that contains an apostrophe's and spans multiple lines without causing errors.
# Option 3: Remove quotes entirely for simple strings
simpleTitle: My Page Title
---

Ensure all indentation in your frontmatter uses the same character type (preferably spaces) and the same number of spaces per level (typically 2 or 4):

---
meta:
title: My Page
description: This is my page
social:
image: ./social.jpg
alt: Social image
---

For complex frontmatter, use an online YAML validator to check your syntax before running your Astro build:

  1. Copy your frontmatter (without the triple dashes)
  2. Paste it into a YAML validator like YAML Lint
  3. Fix any errors highlighted by the validator

Sometimes, invisible characters like zero-width spaces can sneak into your code, especially when copying from websites or documents:

  1. Open your file in a code editor that can show invisible characters
  2. Look for any unusual spacing or characters
  3. Retype the problematic lines from scratch instead of copying/pasting

Lists in YAML require consistent formatting:

---
# Incorrect
tags:
- astro
- javascript # Wrong indentation for nested item
- css
# Correct
tags:
- astro
- javascript
- css
# Also correct (alternative syntax)
tags: [astro, javascript, css]
---

When working with deeply nested objects, maintain consistent indentation at each level:

---
# Correct complex nesting
project:
details:
client:
name: Acme Corp
contact:
phone: 555-1234
timeline:
start: 2025-01-01
end: 2025-06-30
---

By understanding these common causes and solutions, you can quickly resolve the “bad indentation of a mapping entry” error in your Astro projects. Remember that YAML is strict about formatting, so consistent indentation and proper handling of special characters are essential for smooth development.

When in doubt, simplify your frontmatter structure or use a YAML validator to catch issues before they cause build errors. With these techniques, you’ll spend less time debugging and more time building amazing websites with AstroJS.

Related Articles

Read more Debug articles
North Star Themes Logo

Subscribe to our newsletter

Get the latest AstroJS tips and tricks right to your inbox

Email: [email protected]

© 2025 North Star Themes

Web Kit Provided By North Star Themes