How to use a custom pages directory `(outside app/pages)` in Nuxt 4?
Hey everyone 👋
I'm currently exploring Nuxt 4 and trying to organize my project structure using a modular, feature-based layout.
Here’s my current folder structure:
I want Nuxt to automatically generate routes from
app/common/pages instead of app/pages.
So I tried the following config:
But Nuxt still shows this warning:
It seems like the router is enabled, but the files inside app/common/pages aren’t being picked up.
Is pages.pattern supposed to work outside of app/pages/ in Nuxt 4,
or does it only filter files inside app/pages/?
If it’s not supported yet, is there a recommended way to achieve this structure (like aliasing or extending the route generation manually)?
Thanks in advance! 🙏4 Replies
Hi @Dan. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
----------------------
Based on the knowledge sources, the
pages.pattern configuration option is designed to filter files within the default pages directory, not to change the pages directory location itself.
According to the Nuxt configuration documentation, the pages.pattern option allows you to "provide a glob pattern or an array of patterns to scan only certain files for pages." The example shows:
This pattern is relative to the pages directory itself, not the entire app structure.
Solution: Change the Pages Directory Location
To use a custom pages directory like app/common/pages, you need to configure the dir.pages option in your nuxt.config.ts:
According to the directory configuration documentation, dir.pages is "the directory which will be processed to auto-generate your application page routes" with a default value of "app/pages".
Alternative: Multiple Pages Directories with Layers
If you want to include pages from multiple directories (like both app/common/pages and app/features/auth/login/pages), you should use Nuxt Layers. However, this would require restructuring your project to have separate layer configurations.
Manual Route Extension
For more complex scenarios where you need full control over route generation from multiple directories, you can use the pages:extend hook to manually add routes:
However, this approach requires manually defining each route, which may not be ideal for your modular structure.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------<@867041700542152735> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
can component achieve the same thing?
@kapa.ai
Let me check the knowledge base for you again. One moment! :books:
Sorry @Dan, I seem to be having a bit of trouble. Don't worry though, I've pinged my support and they will try to fix me up! :technologist:
Perhaps try again in 15mins?