Newsletter
Join the Community
Subscribe to our newsletter for the latest news and updates
Run sync or async initialization steps at build-time with zero config, overwriting runtime config and passing state.
Nuxt Prepare is a Nuxt module that runs synchronous or asynchronous initialization steps at build-time, letting developers fetch data, overwrite runtime config, and prepare application state before the app is built.
Nuxt Prepare is a zero-config Nuxt module that executes prepare scripts during the Nuxt build process. It takes a handler function defined in server.prepare.ts as input, runs it at build time, and returns runtime config overrides and custom state that get passed to your Nuxt application. The module is developed by Johann Schopplich and is available on npm under the name nuxt-prepare.
It works with any Nuxt project, including those using Nitro as the server engine, and integrates directly with the Nuxt module system. The module is open-source under the MIT license and is actively maintained since 2023.
'nuxt-prepare' to the modules array in nuxt.config.ts and the module automatically looks for a server.prepare.ts file in your project root; no additional configuration required.runtimeConfig object from your prepare handler to overwrite specific values (e.g. public.foo) at build time, allowing environment-specific configuration.state object that becomes importable anywhere in your app via #nuxt-prepare, letting you share build-time data with components, composables, or pages.prepare.scripts in module options; they can run in series or in parallel according to your setup.defineNuxtPrepareHandler from nuxt-prepare/config to give typed hints and autocompletion when writing your prepare handler.Nuxt Prepare is for Nuxt developers who need to run logic at build time rather than at runtime. Use cases include fetching remote data during the build, injecting environment-specific config, and preparing static data for server routes.
runtimeConfig values per build (e.g. staging vs production) by returning different values from the prepare handler.server.prepare.ts handler, call an external API and return the result as state; then import that state anywhere in your Nuxt app using #nuxt-prepare.runtimeConfig.public values to build-specific values, enabling different feature flags or API endpoints without changing code.prepare.scripts and control whether they run in series or parallel, useful for complex build pipelines.The module hooks into Nuxt's build lifecycle. After adding the module and creating server.prepare.ts, define a default exported handler using defineNuxtPrepareHandler. When Nuxt builds your app, the handler executes, and its returned runtimeConfig and state are merged into your Nuxt app. You can then import the state anywhere via #nuxt-prepare alias. Scripts can be registered under prepare.scripts for more advanced orchestration.
