Reading file from folder, but path is different in dev/prod
We have a json file that is generated on build containing our route info. In a request we read the generated file, fetch some data from the backend and return combined json. But we noticed that after deploying the app, path to the generated file is different. Is there a way to use some nuxt/nitro helper so that file location is always resolved correctly?
5 Replies
Hi @Daugvinas. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
----------------------
Yes, Nuxt provides utilities to help resolve file paths in a way that works consistently between development and production. The recommended approach is to use the createResolver function from @nuxt/kit, which allows you to resolve paths relative to a base (such as your module or project directory). This ensures that your file paths are correct regardless of the environment.
Here’s how you can use it:
This will resolve the path relative to the file where this code is written, making it robust across different environments (dev/prod) and build outputs. If you are working inside a Nuxt module or a server handler, this is the recommended way to handle file paths to avoid discrepancies between environments createResolver docs.
If you need to resolve files within modules or runtime code, you can also use import.meta.url as the base for createResolver, as shown in the documentation import.meta docs.
This approach should help you avoid issues with differing file paths between development and production.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------
<@136154943961694208> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
@kapa.ai importing nuxt/kit module is not allowed in server runtime
Let me check the knowledge base for you again. One moment! :books:
----------------------
You are correct: importing from @nuxt/kit (or nuxt/kit) is not allowed in server runtime code such as server routes, server middleware, or API handlers. The Nuxt Kit utilities are designed specifically for use in Nuxt modules and build-time configuration, not for runtime usage. Attempting to import @nuxt/kit in runtime code will result in errors, as Nuxt Kit is an ESM-only package and is not bundled for runtime environments Nuxt Kit Guide.
If you need to resolve file paths at runtime (for example, inside a server route), you should use standard Node.js utilities like import.meta.url, path, or URL APIs, rather than relying on Nuxt Kit helpers. The Kit helpers (such as createResolver) are only available during module setup and build time, not at runtime.
Summary:
- Use @nuxt/kit only in Nuxt modules and build-time code. - Do not import @nuxt/kit in server runtime code (server/api, server/routes, etc.). - For runtime path resolution, use Node.js APIs. Nuxt Kit utilities are only available for modules and not meant to be imported in runtime (components, Vue composables, pages, plugins, or server routes). ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
- Use @nuxt/kit only in Nuxt modules and build-time code. - Do not import @nuxt/kit in server runtime code (server/api, server/routes, etc.). - For runtime path resolution, use Node.js APIs. Nuxt Kit utilities are only available for modules and not meant to be imported in runtime (components, Vue composables, pages, plugins, or server routes). ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
<@136154943961694208> Kapa.ai is still learning and improving, please let me know how I did by reacting below.