#Runs fine in npm run dev but fails in npm run build

4 messages · Page 1 of 1 (latest)

fading fulcrum
#

My site runs fine with "npm run dev" but the dist folder creation fails when I do an "npm run build". Shouldn't it run fine in build if it runs fine in dev?
14:28:19 ▶ /articles/[slug]
14:28:19 [ERROR] [build] Failed to call getStaticPaths for /articles/[slug]
[GetStaticPathsRequired] getStaticPaths() function is required for dynamic routes.

steel pikeBOT
#

Hi @fading fulcrum. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

The error you’re seeing—[GetStaticPathsRequired] getStaticPaths() function is required for dynamic routes—means that in static build mode, Astro needs to know all possible routes at build time for any dynamic route (like /articles/[slug]). While npm run dev works by resolving routes on demand, npm run build (static mode) requires you to export a getStaticPaths() function from your dynamic route file to generate all the necessary pages ahead of time.

To fix this, ensure that your src/pages/articles/[slug].astro (or similar dynamic route file) exports a getStaticPaths() function that returns an array of all possible slugs. For example:

---
// Example: src/pages/articles/[slug].astro
export async function getStaticPaths() {
  // Fetch your articles here and return an array of objects:
  return [
    { params: { slug: 'first-article' } },
    { params: { slug: 'second-article' } },
    // ...more articles
  ];
}
---

Without this function, Astro cannot generate the static pages for each article, which is why the build fails, even though development mode works fine. This is a common requirement in Astro’s static site generation workflow GetStaticPathsRequired error docs.

For more details and examples, see the official documentation on dynamic routes and getStaticPaths.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:

#

@fading fulcrum Kapa.ai is still learning and improving, please let me know how I did by reacting below.