@nuxt/docs 4.1.2 → 4.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (201) hide show
  1. package/1.getting-started/01.introduction.md +6 -6
  2. package/1.getting-started/02.installation.md +3 -2
  3. package/1.getting-started/03.configuration.md +36 -36
  4. package/1.getting-started/04.views.md +9 -9
  5. package/1.getting-started/05.assets.md +15 -9
  6. package/1.getting-started/06.styling.md +58 -48
  7. package/1.getting-started/07.routing.md +17 -17
  8. package/1.getting-started/08.seo-meta.md +59 -46
  9. package/1.getting-started/09.transitions.md +49 -44
  10. package/1.getting-started/10.data-fetching.md +104 -81
  11. package/1.getting-started/11.state-management.md +26 -19
  12. package/1.getting-started/12.error-handling.md +22 -20
  13. package/1.getting-started/13.server.md +8 -8
  14. package/1.getting-started/14.layers.md +22 -16
  15. package/1.getting-started/15.prerendering.md +32 -32
  16. package/1.getting-started/16.deployment.md +10 -10
  17. package/1.getting-started/17.testing.md +44 -44
  18. package/1.getting-started/18.upgrade.md +241 -110
  19. package/2.guide/0.index.md +7 -7
  20. package/2.guide/{2.directory-structure → 1.directory-structure}/.navigation.yml +1 -1
  21. package/2.guide/{2.directory-structure → 1.directory-structure}/0.nuxt.md +3 -3
  22. package/2.guide/1.directory-structure/0.output.md +18 -0
  23. package/2.guide/{2.directory-structure → 1.directory-structure}/1.app/.navigation.yml +1 -1
  24. package/2.guide/{2.directory-structure → 1.directory-structure}/1.app/1.assets.md +4 -4
  25. package/2.guide/{2.directory-structure → 1.directory-structure}/1.app/1.components.md +45 -28
  26. package/2.guide/{2.directory-structure → 1.directory-structure}/1.app/1.composables.md +13 -13
  27. package/2.guide/{2.directory-structure → 1.directory-structure}/1.app/1.layouts.md +19 -15
  28. package/2.guide/{2.directory-structure → 1.directory-structure}/1.app/1.middleware.md +31 -25
  29. package/2.guide/{2.directory-structure → 1.directory-structure}/1.app/1.pages.md +39 -37
  30. package/2.guide/{2.directory-structure → 1.directory-structure}/1.app/1.plugins.md +25 -25
  31. package/2.guide/{2.directory-structure → 1.directory-structure}/1.app/1.utils.md +7 -7
  32. package/2.guide/{2.directory-structure → 1.directory-structure}/1.app/3.app-config.md +18 -18
  33. package/2.guide/{2.directory-structure → 1.directory-structure}/1.app/3.app.md +7 -7
  34. package/2.guide/{2.directory-structure → 1.directory-structure}/1.app/3.error.md +6 -6
  35. package/2.guide/{2.directory-structure → 1.directory-structure}/1.content.md +7 -4
  36. package/2.guide/{2.directory-structure → 1.directory-structure}/1.modules.md +8 -8
  37. package/2.guide/{2.directory-structure → 1.directory-structure}/1.node_modules.md +2 -2
  38. package/2.guide/{2.directory-structure → 1.directory-structure}/1.public.md +2 -2
  39. package/2.guide/{2.directory-structure → 1.directory-structure}/1.server.md +35 -35
  40. package/2.guide/{2.directory-structure → 1.directory-structure}/1.shared.md +7 -7
  41. package/2.guide/{2.directory-structure → 1.directory-structure}/2.env.md +8 -8
  42. package/2.guide/{2.directory-structure → 1.directory-structure}/2.gitignore.md +1 -1
  43. package/2.guide/{2.directory-structure → 1.directory-structure}/2.nuxtignore.md +5 -4
  44. package/2.guide/{2.directory-structure → 1.directory-structure}/2.nuxtrc.md +4 -4
  45. package/2.guide/{2.directory-structure → 1.directory-structure}/3.nuxt-config.md +3 -3
  46. package/2.guide/{2.directory-structure → 1.directory-structure}/3.package.md +1 -1
  47. package/2.guide/{2.directory-structure → 1.directory-structure}/3.tsconfig.md +3 -3
  48. package/2.guide/{1.concepts → 2.concepts}/1.auto-imports.md +22 -22
  49. package/2.guide/{1.concepts → 2.concepts}/10.nuxt-lifecycle.md +17 -17
  50. package/2.guide/{1.concepts → 2.concepts}/2.vuejs-development.md +13 -13
  51. package/2.guide/{1.concepts → 2.concepts}/3.rendering.md +24 -22
  52. package/2.guide/{1.concepts → 2.concepts}/4.server-engine.md +6 -6
  53. package/2.guide/{1.concepts → 2.concepts}/5.modules.md +5 -5
  54. package/2.guide/{1.concepts → 2.concepts}/7.esm.md +22 -16
  55. package/2.guide/{1.concepts → 2.concepts}/8.typescript.md +12 -12
  56. package/2.guide/3.going-further/1.events.md +3 -3
  57. package/2.guide/3.going-further/1.experimental-features.md +334 -85
  58. package/2.guide/3.going-further/1.features.md +58 -11
  59. package/2.guide/3.going-further/1.internals.md +25 -25
  60. package/2.guide/3.going-further/10.runtime-config.md +12 -12
  61. package/2.guide/3.going-further/11.nightly-release-channel.md +1 -1
  62. package/2.guide/3.going-further/2.hooks.md +14 -14
  63. package/2.guide/3.going-further/3.modules.md +126 -108
  64. package/2.guide/3.going-further/4.kit.md +7 -7
  65. package/2.guide/3.going-further/6.nuxt-app.md +8 -8
  66. package/2.guide/3.going-further/7.layers.md +76 -59
  67. package/2.guide/3.going-further/9.debugging.md +2 -2
  68. package/2.guide/4.recipes/1.custom-routing.md +30 -30
  69. package/2.guide/4.recipes/2.vite-plugin.md +45 -4
  70. package/2.guide/4.recipes/3.custom-usefetch.md +13 -13
  71. package/2.guide/4.recipes/4.sessions-and-authentication.md +35 -21
  72. package/2.guide/5.best-practices/hydration.md +4 -4
  73. package/2.guide/5.best-practices/performance.md +17 -17
  74. package/3.api/1.components/1.client-only.md +6 -3
  75. package/3.api/1.components/1.nuxt-client-fallback.md +10 -7
  76. package/3.api/1.components/10.nuxt-picture.md +1 -1
  77. package/3.api/1.components/11.teleports.md +5 -2
  78. package/3.api/1.components/12.nuxt-route-announcer.md +9 -9
  79. package/3.api/1.components/13.nuxt-time.md +49 -18
  80. package/3.api/1.components/2.nuxt-page.md +6 -6
  81. package/3.api/1.components/3.nuxt-layout.md +15 -10
  82. package/3.api/1.components/4.nuxt-link.md +42 -22
  83. package/3.api/1.components/5.nuxt-loading-indicator.md +3 -3
  84. package/3.api/1.components/6.nuxt-error-boundary.md +12 -10
  85. package/3.api/2.composables/on-prehydrate.md +2 -2
  86. package/3.api/2.composables/use-app-config.md +1 -1
  87. package/3.api/2.composables/use-async-data.md +26 -22
  88. package/3.api/2.composables/use-cookie.md +29 -21
  89. package/3.api/2.composables/use-error.md +2 -2
  90. package/3.api/2.composables/use-fetch.md +62 -32
  91. package/3.api/2.composables/use-head-safe.md +7 -7
  92. package/3.api/2.composables/use-head.md +5 -5
  93. package/3.api/2.composables/use-hydration.md +6 -6
  94. package/3.api/2.composables/use-lazy-async-data.md +4 -4
  95. package/3.api/2.composables/use-lazy-fetch.md +4 -4
  96. package/3.api/2.composables/use-loading-indicator.md +12 -12
  97. package/3.api/2.composables/use-nuxt-app.md +22 -22
  98. package/3.api/2.composables/use-nuxt-data.md +8 -8
  99. package/3.api/2.composables/use-preview-mode.md +15 -18
  100. package/3.api/2.composables/use-request-event.md +1 -1
  101. package/3.api/2.composables/use-request-fetch.md +3 -3
  102. package/3.api/2.composables/use-request-header.md +1 -1
  103. package/3.api/2.composables/use-request-headers.md +4 -4
  104. package/3.api/2.composables/use-request-url.md +1 -1
  105. package/3.api/2.composables/use-response-header.md +9 -10
  106. package/3.api/2.composables/use-route-announcer.md +4 -4
  107. package/3.api/2.composables/use-route.md +1 -1
  108. package/3.api/2.composables/use-router.md +10 -8
  109. package/3.api/2.composables/use-runtime-config.md +11 -11
  110. package/3.api/2.composables/use-runtime-hook.md +2 -2
  111. package/3.api/2.composables/use-seo-meta.md +4 -4
  112. package/3.api/2.composables/use-server-seo-meta.md +6 -6
  113. package/3.api/2.composables/use-state.md +5 -5
  114. package/3.api/3.utils/$fetch.md +10 -8
  115. package/3.api/3.utils/abort-navigation.md +3 -3
  116. package/3.api/3.utils/add-route-middleware.md +5 -5
  117. package/3.api/3.utils/call-once.md +6 -6
  118. package/3.api/3.utils/clear-error.md +3 -3
  119. package/3.api/3.utils/clear-nuxt-data.md +3 -3
  120. package/3.api/3.utils/clear-nuxt-state.md +3 -3
  121. package/3.api/3.utils/create-error.md +2 -2
  122. package/3.api/3.utils/define-lazy-hydration-component.md +13 -13
  123. package/3.api/3.utils/define-nuxt-component.md +5 -5
  124. package/3.api/3.utils/define-nuxt-plugin.md +13 -13
  125. package/3.api/3.utils/define-nuxt-route-middleware.md +5 -5
  126. package/3.api/3.utils/define-page-meta.md +23 -23
  127. package/3.api/3.utils/define-route-rules.md +7 -7
  128. package/3.api/3.utils/navigate-to.md +11 -11
  129. package/3.api/3.utils/prefetch-components.md +1 -1
  130. package/3.api/3.utils/preload-components.md +1 -1
  131. package/3.api/3.utils/preload-route-components.md +2 -2
  132. package/3.api/3.utils/prerender-routes.md +3 -3
  133. package/3.api/3.utils/refresh-cookie.md +4 -4
  134. package/3.api/3.utils/refresh-nuxt-data.md +12 -7
  135. package/3.api/3.utils/reload-nuxt-app.md +3 -3
  136. package/3.api/3.utils/set-page-layout.md +1 -1
  137. package/3.api/3.utils/set-response-status.md +3 -3
  138. package/3.api/3.utils/show-error.md +5 -5
  139. package/3.api/3.utils/update-app-config.md +4 -3
  140. package/3.api/4.commands/add.md +1 -1
  141. package/3.api/4.commands/analyze.md +2 -1
  142. package/3.api/4.commands/build.md +2 -1
  143. package/3.api/4.commands/dev.md +5 -4
  144. package/3.api/4.commands/generate.md +3 -2
  145. package/3.api/4.commands/init.md +3 -2
  146. package/3.api/4.commands/module.md +4 -4
  147. package/3.api/4.commands/prepare.md +7 -2
  148. package/3.api/4.commands/preview.md +5 -4
  149. package/3.api/4.commands/test.md +40 -0
  150. package/3.api/4.commands/typecheck.md +5 -3
  151. package/3.api/4.commands/upgrade.md +3 -3
  152. package/3.api/5.kit/1.modules.md +123 -37
  153. package/3.api/5.kit/10.runtime-config.md +1 -1
  154. package/3.api/5.kit/10.templates.md +8 -6
  155. package/3.api/5.kit/11.nitro.md +66 -62
  156. package/3.api/5.kit/12.resolving.md +2 -2
  157. package/3.api/5.kit/14.builder.md +61 -4
  158. package/3.api/5.kit/15.examples.md +5 -7
  159. package/3.api/5.kit/16.layers.md +26 -26
  160. package/3.api/5.kit/3.compatibility.md +14 -14
  161. package/3.api/5.kit/4.autoimports.md +13 -13
  162. package/3.api/5.kit/5.components.md +8 -7
  163. package/3.api/5.kit/6.context.md +3 -3
  164. package/3.api/5.kit/7.pages.md +7 -7
  165. package/3.api/5.kit/8.layout.md +2 -2
  166. package/3.api/5.kit/9.head.md +132 -0
  167. package/3.api/5.kit/9.plugins.md +5 -4
  168. package/3.api/6.advanced/1.hooks.md +5 -5
  169. package/3.api/6.advanced/2.import-meta.md +3 -3
  170. package/3.api/6.nuxt-config.md +365 -859
  171. package/3.api/index.md +7 -7
  172. package/5.community/2.getting-help.md +1 -1
  173. package/5.community/3.reporting-bugs.md +3 -3
  174. package/5.community/4.contribution.md +11 -11
  175. package/5.community/5.framework-contribution.md +4 -4
  176. package/5.community/6.roadmap.md +2 -2
  177. package/6.bridge/1.overview.md +13 -13
  178. package/6.bridge/10.configuration.md +2 -1
  179. package/6.bridge/2.typescript.md +3 -3
  180. package/6.bridge/3.bridge-composition-api.md +8 -8
  181. package/6.bridge/4.plugins-and-middleware.md +9 -9
  182. package/6.bridge/5.nuxt3-compatible-api.md +20 -17
  183. package/6.bridge/6.meta.md +20 -19
  184. package/6.bridge/7.runtime-config.md +1 -1
  185. package/6.bridge/8.nitro.md +3 -3
  186. package/6.bridge/9.vite.md +4 -4
  187. package/7.migration/1.overview.md +2 -2
  188. package/7.migration/10.bundling.md +1 -1
  189. package/7.migration/11.server.md +3 -3
  190. package/7.migration/2.configuration.md +23 -21
  191. package/7.migration/20.module-authors.md +7 -7
  192. package/7.migration/3.auto-imports.md +3 -3
  193. package/7.migration/4.meta.md +21 -18
  194. package/7.migration/5.plugins-and-middleware.md +8 -8
  195. package/7.migration/6.pages-and-layouts.md +28 -24
  196. package/7.migration/7.component-options.md +18 -18
  197. package/7.migration/8.runtime-config.md +7 -7
  198. package/package.json +1 -1
  199. package/2.guide/2.directory-structure/0.output.md +0 -18
  200. /package/2.guide/{1.concepts → 2.concepts}/.navigation.yml +0 -0
  201. /package/2.guide/{1.concepts → 2.concepts}/9.code-style.md +0 -0
@@ -4,7 +4,7 @@ description: "Learn how to create a Nuxt Module to integrate, enhance or extend
4
4
  image: '/socials/module-author-guide.jpg'
5
5
  ---
6
6
 
7
- Nuxt's [configuration](/docs/api/nuxt-config) and [hooks](/docs/guide/going-further/hooks) systems make it possible to customize every aspect of Nuxt and add any integration you might need (Vue plugins, CMS, server routes, components, logging, etc.).
7
+ Nuxt's [configuration](/docs/4.x/api/nuxt-config) and [hooks](/docs/4.x/guide/going-further/hooks) systems make it possible to customize every aspect of Nuxt and add any integration you might need (Vue plugins, CMS, server routes, components, logging, etc.).
8
8
 
9
9
  **Nuxt Modules** are functions that sequentially run when starting Nuxt in development mode using `nuxt dev` or building a project for production with `nuxt build`.
10
10
  With modules, you can encapsulate, properly test, and share custom solutions as npm packages without adding unnecessary boilerplate to your project, or requiring changes to Nuxt itself.
@@ -117,7 +117,7 @@ Nuxt Modules come with a variety of powerful APIs and patterns allowing them to
117
117
  We can consider two kinds of Nuxt Modules:
118
118
 
119
119
  - published modules are distributed on npm - you can see a list of some community modules on [the Nuxt website](/modules).
120
- - "local" modules, they exist within a Nuxt project itself, either [inlined in Nuxt config](/docs/api/nuxt-config#modules) or as part of [the `modules` directory](/docs/guide/directory-structure/modules).
120
+ - "local" modules, they exist within a Nuxt project itself, either [inlined in Nuxt config](/docs/4.x/api/nuxt-config#modules) or as part of [the `modules` directory](/docs/4.x/guide/directory-structure/modules).
121
121
 
122
122
  In either case, their anatomy is similar.
123
123
 
@@ -136,19 +136,19 @@ export default function (inlineOptions, nuxt) {
136
136
  // You can do whatever you like here..
137
137
  console.log(inlineOptions.token) // `123`
138
138
  console.log(nuxt.options.dev) // `true` or `false`
139
- nuxt.hook('ready', async nuxt => {
139
+ nuxt.hook('ready', (nuxt) => {
140
140
  console.log('Nuxt is ready')
141
141
  })
142
142
  }
143
143
  ```
144
144
 
145
- You can get type-hint support for this function using the higher-level `defineNuxtModule` helper provided by [Nuxt Kit](/docs/guide/going-further/kit).
145
+ You can get type-hint support for this function using the higher-level `defineNuxtModule` helper provided by [Nuxt Kit](/docs/4.x/guide/going-further/kit).
146
146
 
147
147
  ```ts
148
148
  import { defineNuxtModule } from '@nuxt/kit'
149
149
 
150
150
  export default defineNuxtModule((options, nuxt) => {
151
- nuxt.hook('pages:extend', pages => {
151
+ nuxt.hook('pages:extend', (pages) => {
152
152
  console.log(`Discovered ${pages.length} pages`)
153
153
  })
154
154
  })
@@ -170,8 +170,8 @@ export default defineNuxtModule({
170
170
  // Compatibility constraints
171
171
  compatibility: {
172
172
  // Semver version of supported nuxt versions
173
- nuxt: '>=3.0.0'
174
- }
173
+ nuxt: '>=3.0.0',
174
+ },
175
175
  },
176
176
  // Default configuration options for your module, can also be a function returning those
177
177
  defaults: {},
@@ -191,13 +191,13 @@ export default defineNuxtModule({
191
191
  overrides: {},
192
192
  // Any configuration that should be set. It will override module defaults but
193
193
  // will not override any configuration set in `nuxt.options`.
194
- defaults: {}
195
- }
194
+ defaults: {},
195
+ },
196
196
  },
197
197
  // The function holding your module logic, it can be asynchronous
198
- setup(moduleOptions, nuxt) {
198
+ setup (moduleOptions, nuxt) {
199
199
  // ...
200
- }
200
+ },
201
201
  })
202
202
  ```
203
203
 
@@ -224,9 +224,9 @@ Modules, like everything in a Nuxt configuration, aren't included in your applic
224
224
  Inside the runtime directory, you can provide any kind of assets related to the Nuxt App:
225
225
  - Vue components
226
226
  - Composables
227
- - [Nuxt plugins](/docs/guide/directory-structure/plugins)
227
+ - [Nuxt plugins](/docs/4.x/guide/directory-structure/plugins)
228
228
 
229
- To the [server engine](/docs/guide/concepts/server-engine), Nitro:
229
+ To the [server engine](/docs/4.x/guide/concepts/server-engine), Nitro:
230
230
  - API routes
231
231
  - Middlewares
232
232
  - Nitro plugins
@@ -237,10 +237,10 @@ Or any other kind of asset you want to inject in users' Nuxt applications:
237
237
  - Images
238
238
  - etc.
239
239
 
240
- You'll then be able to inject all those assets inside the application from your [module definition](#module-definition).
240
+ You'll then be able to inject all those assets inside the application from your [module definition](/docs/4.x/guide/going-further/modules#module-definition).
241
241
 
242
242
  ::tip
243
- Learn more about asset injection in [the recipes section](#recipes).
243
+ Learn more about asset injection in [the recipes section](/docs/4.x/guide/going-further/modules#recipes).
244
244
  ::
245
245
 
246
246
  ::warning
@@ -259,13 +259,13 @@ Modules come with a set of first-party tools to help you with their development.
259
259
 
260
260
  #### `@nuxt/kit`
261
261
 
262
- [Nuxt Kit](/docs/guide/going-further/kit) provides composable utilities to help your module interact with Nuxt applications. It's recommended to use Nuxt Kit utilities over manual alternatives whenever possible to ensure better compatibility and code readability of your module.
262
+ [Nuxt Kit](/docs/4.x/guide/going-further/kit) provides composable utilities to help your module interact with Nuxt applications. It's recommended to use Nuxt Kit utilities over manual alternatives whenever possible to ensure better compatibility and code readability of your module.
263
263
 
264
- :read-more{to="/docs/guide/going-further/kit"}
264
+ :read-more{to="/docs/4.x/guide/going-further/kit"}
265
265
 
266
266
  #### `@nuxt/test-utils`
267
267
 
268
- [Nuxt Test Utils](/docs/getting-started/testing) is a collection of utilities to help set up and run Nuxt applications within your module tests.
268
+ [Nuxt Test Utils](/docs/4.x/getting-started/testing) is a collection of utilities to help set up and run Nuxt applications within your module tests.
269
269
 
270
270
  ### Recipes
271
271
 
@@ -283,7 +283,7 @@ export default defineNuxtModule({
283
283
  // We create the `experimental` object if it doesn't exist yet
284
284
  nuxt.options.experimental ||= {}
285
285
  nuxt.options.experimental.componentIslands = true
286
- }
286
+ },
287
287
  })
288
288
  ```
289
289
 
@@ -295,7 +295,7 @@ Watch Vue School video about altering Nuxt configuration.
295
295
 
296
296
  #### Exposing Options to Runtime
297
297
 
298
- Because modules aren't part of the application runtime, their options aren't either. However, in many cases, you might need access to some of these module options within your runtime code. We recommend exposing the needed config using Nuxt's [`runtimeConfig`](/docs/api/nuxt-config#runtimeconfig).
298
+ Because modules aren't part of the application runtime, their options aren't either. However, in many cases, you might need access to some of these module options within your runtime code. We recommend exposing the needed config using Nuxt's [`runtimeConfig`](/docs/4.x/api/nuxt-config#runtimeconfig).
299
299
 
300
300
  <!-- TODO: Update after #18466 (or equivalent) -->
301
301
 
@@ -306,9 +306,9 @@ import { defu } from 'defu'
306
306
  export default defineNuxtModule({
307
307
  setup (options, nuxt) {
308
308
  nuxt.options.runtimeConfig.public.myModule = defu(nuxt.options.runtimeConfig.public.myModule, {
309
- foo: options.foo
309
+ foo: options.foo,
310
310
  })
311
- }
311
+ },
312
312
  })
313
313
  ```
314
314
 
@@ -317,6 +317,8 @@ Note that we use [`defu`](https://github.com/unjs/defu) to extend the public run
317
317
  You can then access your module options in a plugin, component, the application like any other runtime configuration:
318
318
 
319
319
  ```js
320
+ import { useRuntimeConfig } from '@nuxt/kit'
321
+
320
322
  const options = useRuntimeConfig().public.myModule
321
323
  ```
322
324
 
@@ -324,7 +326,7 @@ const options = useRuntimeConfig().public.myModule
324
326
  Be careful not to expose any sensitive module configuration on the public runtime config, such as private API keys, as they will end up in the public bundle.
325
327
  ::
326
328
 
327
- :read-more{to="/docs/guide/going-further/runtime-config"}
329
+ :read-more{to="/docs/4.x/guide/going-further/runtime-config"}
328
330
 
329
331
  ::tip{icon="i-lucide-video" to="https://vueschool.io/lessons/passing-and-exposing-module-options?friend=nuxt" target="_blank"}
330
332
  Watch Vue School video about passing and exposing Nuxt module options.
@@ -335,7 +337,7 @@ Watch Vue School video about passing and exposing Nuxt module options.
335
337
  Plugins are a common way for a module to add runtime logic. You can use the `addPlugin` utility to register them from your module.
336
338
 
337
339
  ```js
338
- import { defineNuxtModule, addPlugin, createResolver } from '@nuxt/kit'
340
+ import { addPlugin, createResolver, defineNuxtModule } from '@nuxt/kit'
339
341
 
340
342
  export default defineNuxtModule({
341
343
  setup (options, nuxt) {
@@ -343,53 +345,53 @@ export default defineNuxtModule({
343
345
  const resolver = createResolver(import.meta.url)
344
346
 
345
347
  addPlugin(resolver.resolve('./runtime/plugin'))
346
- }
348
+ },
347
349
  })
348
350
  ```
349
351
 
350
- :read-more{to="/docs/guide/going-further/kit"}
352
+ :read-more{to="/docs/4.x/guide/going-further/kit"}
351
353
 
352
354
  #### Injecting Vue Components With `addComponent`
353
355
 
354
356
  If your module should provide Vue components, you can use the `addComponent` utility to add them as auto-imports for Nuxt to resolve.
355
357
 
356
- ```js
357
- import { defineNuxtModule, addComponent } from '@nuxt/kit'
358
+ ```ts twoslash
359
+ import { addComponent, createResolver, defineNuxtModule, useRuntimeConfig } from '@nuxt/kit'
358
360
 
359
361
  export default defineNuxtModule({
360
- setup(options, nuxt) {
362
+ setup (options, nuxt) {
361
363
  const resolver = createResolver(import.meta.url)
362
364
 
363
365
  // From the runtime directory
364
366
  addComponent({
365
367
  name: 'MySuperComponent', // name of the component to be used in vue templates
366
368
  export: 'MySuperComponent', // (optional) if the component is a named (rather than default) export
367
- filePath: resolver.resolve('runtime/components/MySuperComponent.vue')
369
+ filePath: resolver.resolve('runtime/components/MySuperComponent.vue'),
368
370
  })
369
371
 
370
372
  // From a library
371
373
  addComponent({
372
374
  name: 'MyAwesomeComponent', // name of the component to be used in vue templates
373
375
  export: 'MyAwesomeComponent', // (optional) if the component is a named (rather than default) export
374
- filePath: '@vue/awesome-components'
376
+ filePath: '@vue/awesome-components',
375
377
  })
376
- }
378
+ },
377
379
  })
378
380
  ```
379
381
 
380
382
  Alternatively, you can add an entire directory by using `addComponentsDir`.
381
383
 
382
384
  ```ts
383
- import { defineNuxtModule, addComponentsDir } from '@nuxt/kit'
385
+ import { addComponentsDir, defineNuxtModule } from '@nuxt/kit'
384
386
 
385
387
  export default defineNuxtModule({
386
- setup(options, nuxt) {
388
+ setup (options, nuxt) {
387
389
  const resolver = createResolver(import.meta.url)
388
390
 
389
391
  addComponentsDir({
390
- path: resolver.resolve('runtime/components')
392
+ path: resolver.resolve('runtime/components'),
391
393
  })
392
- }
394
+ },
393
395
  })
394
396
  ```
395
397
 
@@ -398,66 +400,66 @@ export default defineNuxtModule({
398
400
  If your module should provide composables, you can use the `addImports` utility to add them as auto-imports for Nuxt to resolve.
399
401
 
400
402
  ```ts
401
- import { defineNuxtModule, addImports, createResolver } from '@nuxt/kit'
403
+ import { addImports, createResolver, defineNuxtModule } from '@nuxt/kit'
402
404
 
403
405
  export default defineNuxtModule({
404
- setup(options, nuxt) {
406
+ setup (options, nuxt) {
405
407
  const resolver = createResolver(import.meta.url)
406
408
 
407
409
  addImports({
408
410
  name: 'useComposable', // name of the composable to be used
409
411
  as: 'useComposable',
410
- from: resolver.resolve('runtime/composables/useComposable') // path of composable
412
+ from: resolver.resolve('runtime/composables/useComposable'), // path of composable
411
413
  })
412
- }
414
+ },
413
415
  })
414
416
  ```
415
417
 
416
418
  Alternatively, you can add an entire directory by using `addImportsDir`.
417
419
 
418
420
  ```ts
419
- import { defineNuxtModule, addImportsDir, createResolver } from '@nuxt/kit'
421
+ import { addImportsDir, createResolver, defineNuxtModule } from '@nuxt/kit'
420
422
 
421
423
  export default defineNuxtModule({
422
- setup(options, nuxt) {
424
+ setup (options, nuxt) {
423
425
  const resolver = createResolver(import.meta.url)
424
426
 
425
427
  addImportsDir(resolver.resolve('runtime/composables'))
426
- }
428
+ },
427
429
  })
428
430
  ```
429
431
 
430
432
  #### Injecting Server Routes With `addServerHandler`
431
433
 
432
434
  ```ts
433
- import { defineNuxtModule, addServerHandler, createResolver } from '@nuxt/kit'
435
+ import { addServerHandler, createResolver, defineNuxtModule } from '@nuxt/kit'
434
436
 
435
437
  export default defineNuxtModule({
436
- setup(options, nuxt) {
438
+ setup (options, nuxt) {
437
439
  const resolver = createResolver(import.meta.url)
438
440
 
439
441
  addServerHandler({
440
442
  route: '/api/hello',
441
- handler: resolver.resolve('./runtime/server/api/hello/index.get')
443
+ handler: resolver.resolve('./runtime/server/api/hello/index.get'),
442
444
  })
443
- }
445
+ },
444
446
  })
445
447
  ```
446
448
 
447
449
  You can also add a dynamic server route:
448
450
 
449
451
  ```ts
450
- import { defineNuxtModule, addServerHandler, createResolver } from '@nuxt/kit'
452
+ import { addServerHandler, createResolver, defineNuxtModule } from '@nuxt/kit'
451
453
 
452
454
  export default defineNuxtModule({
453
- setup(options, nuxt) {
455
+ setup (options, nuxt) {
454
456
  const resolver = createResolver(import.meta.url)
455
457
 
456
458
  addServerHandler({
457
459
  route: '/api/hello/:name',
458
- handler: resolver.resolve('./runtime/server/api/hello/[name].get')
460
+ handler: resolver.resolve('./runtime/server/api/hello/[name].get'),
459
461
  })
460
- }
462
+ },
461
463
  })
462
464
  ```
463
465
 
@@ -466,92 +468,108 @@ export default defineNuxtModule({
466
468
  If your module should provide other kinds of assets, they can also be injected. Here's a simple example module injecting a stylesheet through Nuxt's `css` array.
467
469
 
468
470
  ```js
469
- import { defineNuxtModule, addPlugin, createResolver } from '@nuxt/kit'
471
+ import { addPlugin, createResolver, defineNuxtModule } from '@nuxt/kit'
470
472
 
471
473
  export default defineNuxtModule({
472
474
  setup (options, nuxt) {
473
475
  const resolver = createResolver(import.meta.url)
474
476
 
475
477
  nuxt.options.css.push(resolver.resolve('./runtime/style.css'))
476
- }
478
+ },
477
479
  })
478
480
  ```
479
481
 
480
- And a more advanced one, exposing a folder of assets through [Nitro](/docs/guide/concepts/server-engine)'s `publicAssets` option:
482
+ And a more advanced one, exposing a folder of assets through [Nitro](/docs/4.x/guide/concepts/server-engine)'s `publicAssets` option:
481
483
 
482
484
  ```js
483
- import { defineNuxtModule, createResolver } from '@nuxt/kit'
485
+ import { createResolver, defineNuxtModule } from '@nuxt/kit'
484
486
 
485
487
  export default defineNuxtModule({
486
488
  setup (options, nuxt) {
487
489
  const resolver = createResolver(import.meta.url)
488
490
 
489
- nuxt.hook('nitro:config', async (nitroConfig) => {
491
+ nuxt.hook('nitro:config', (nitroConfig) => {
490
492
  nitroConfig.publicAssets ||= []
491
493
  nitroConfig.publicAssets.push({
492
494
  dir: resolver.resolve('./runtime/public'),
493
- maxAge: 60 * 60 * 24 * 365 // 1 year
495
+ maxAge: 60 * 60 * 24 * 365, // 1 year
494
496
  })
495
497
  })
496
- }
498
+ },
497
499
  })
498
500
  ```
499
501
 
500
502
  #### Using Other Modules in Your Module
501
503
 
502
- If your module depends on other modules, you can add them by using Nuxt Kit's `installModule` utility. For example, if you wanted to use Nuxt Tailwind in your module, you could add it as below:
504
+ If your module depends on other modules, you can specify them using the `moduleDependencies` option. This provides a more robust way to handle module dependencies with version constraints and configuration merging:
503
505
 
504
506
  ```ts
505
- import { defineNuxtModule, createResolver, installModule } from '@nuxt/kit'
507
+ import { createResolver, defineNuxtModule } from '@nuxt/kit'
506
508
 
507
- export default defineNuxtModule<ModuleOptions>({
508
- async setup (options, nuxt) {
509
- const resolver = createResolver(import.meta.url)
509
+ const resolver = createResolver(import.meta.url)
510
510
 
511
+ export default defineNuxtModule<ModuleOptions>({
512
+ meta: {
513
+ name: 'my-module',
514
+ },
515
+ moduleDependencies: {
516
+ '@nuxtjs/tailwindcss': {
517
+ // You can specify a version constraint for the module
518
+ version: '>=6',
519
+ // Any configuration that should override `nuxt.options`
520
+ overrides: {
521
+ exposeConfig: true,
522
+ },
523
+ // Any configuration that should be set. It will override module defaults but
524
+ // will not override any configuration set in `nuxt.options`
525
+ defaults: {
526
+ config: {
527
+ darkMode: 'class',
528
+ content: {
529
+ files: [
530
+ resolver.resolve('./runtime/components/**/*.{vue,mjs,ts}'),
531
+ resolver.resolve('./runtime/*.{mjs,js,ts}'),
532
+ ],
533
+ },
534
+ },
535
+ },
536
+ },
537
+ },
538
+ setup (options, nuxt) {
511
539
  // We can inject our CSS file which includes Tailwind's directives
512
540
  nuxt.options.css.push(resolver.resolve('./runtime/assets/styles.css'))
513
-
514
- await installModule('@nuxtjs/tailwindcss', {
515
- // module configuration
516
- exposeConfig: true,
517
- config: {
518
- darkMode: 'class',
519
- content: {
520
- files: [
521
- resolver.resolve('./runtime/components/**/*.{vue,mjs,ts}'),
522
- resolver.resolve('./runtime/*.{mjs,js,ts}')
523
- ]
524
- }
525
- }
526
- })
527
- }
541
+ },
528
542
  })
529
543
  ```
530
544
 
545
+ ::callout{type="info"}
546
+ The `moduleDependencies` option replaces the deprecated `installModule` function and ensures proper setup order and configuration merging.
547
+ ::
548
+
531
549
  #### Using Hooks
532
550
 
533
- [Lifecycle hooks](/docs/guide/going-further/hooks) allow you to expand almost every aspect of Nuxt. Modules can hook to them programmatically or through the `hooks` map in their definition.
551
+ [Lifecycle hooks](/docs/4.x/guide/going-further/hooks) allow you to expand almost every aspect of Nuxt. Modules can hook to them programmatically or through the `hooks` map in their definition.
534
552
 
535
553
  ```js
536
- import { defineNuxtModule, addPlugin, createResolver } from '@nuxt/kit'
554
+ import { addPlugin, createResolver, defineNuxtModule } from '@nuxt/kit'
537
555
 
538
556
  export default defineNuxtModule({
539
557
  // Hook to the `app:error` hook through the `hooks` map
540
558
  hooks: {
541
559
  'app:error': (err) => {
542
- console.info(`This error happened: ${err}`);
543
- }
560
+ console.info(`This error happened: ${err}`)
561
+ },
544
562
  },
545
563
  setup (options, nuxt) {
546
564
  // Programmatically hook to the `pages:extend` hook
547
565
  nuxt.hook('pages:extend', (pages) => {
548
- console.info(`Discovered ${pages.length} pages`);
566
+ console.info(`Discovered ${pages.length} pages`)
549
567
  })
550
- }
568
+ },
551
569
  })
552
570
  ```
553
571
 
554
- :read-more{to="/docs/api/advanced/hooks"}
572
+ :read-more{to="/docs/4.x/api/advanced/hooks"}
555
573
 
556
574
  ::tip{icon="i-lucide-video" to="https://vueschool.io/lessons/nuxt-lifecycle-hooks?friend=nuxt" target="_blank"}
557
575
  Watch Vue School video about using Nuxt lifecycle hooks in modules.
@@ -568,10 +586,10 @@ import { defineNuxtModule } from '@nuxt/kit'
568
586
 
569
587
  export default defineNuxtModule({
570
588
  setup (options, nuxt) {
571
- nuxt.hook('close', async nuxt => {
589
+ nuxt.hook('close', async (nuxt) => {
572
590
  // Your custom code here
573
591
  })
574
- }
592
+ },
575
593
  })
576
594
  ```
577
595
  ::
@@ -597,7 +615,7 @@ export default defineNuxtModule({
597
615
  const payload = { foo: 'bar' }
598
616
  await nuxt.callHook('my-module:custom-hook', payload)
599
617
  })
600
- }
618
+ },
601
619
  })
602
620
  ```
603
621
 
@@ -606,32 +624,32 @@ export default defineNuxtModule({
606
624
  If you need to add a virtual file that can be imported into the user's app, you can use the `addTemplate` utility.
607
625
 
608
626
  ```ts
609
- import { defineNuxtModule, addTemplate } from '@nuxt/kit'
627
+ import { addTemplate, defineNuxtModule } from '@nuxt/kit'
610
628
 
611
629
  export default defineNuxtModule({
612
630
  setup (options, nuxt) {
613
631
  // The file is added to Nuxt's internal virtual file system and can be imported from '#build/my-module-feature.mjs'
614
632
  addTemplate({
615
633
  filename: 'my-module-feature.mjs',
616
- getContents: () => 'export const myModuleFeature = () => "hello world !"'
634
+ getContents: () => 'export const myModuleFeature = () => "hello world !"',
617
635
  })
618
- }
636
+ },
619
637
  })
620
638
  ```
621
639
 
622
640
  For the server, you should use the `addServerTemplate` utility instead.
623
641
 
624
642
  ```ts
625
- import { defineNuxtModule, addServerTemplate } from '@nuxt/kit'
643
+ import { addServerTemplate, defineNuxtModule } from '@nuxt/kit'
626
644
 
627
645
  export default defineNuxtModule({
628
646
  setup (options, nuxt) {
629
647
  // The file is added to Nitro's virtual file system and can be imported in the server code from 'my-server-module.mjs'
630
648
  addServerTemplate({
631
649
  filename: 'my-server-module.mjs',
632
- getContents: () => 'export const myServerModule = () => "hello world !"'
650
+ getContents: () => 'export const myServerModule = () => "hello world !"',
633
651
  })
634
- }
652
+ },
635
653
  })
636
654
  ```
637
655
 
@@ -644,7 +662,7 @@ writes a template to the disk and adds a reference to it in the generated `nuxt.
644
662
  If your module should augment types handled by Nuxt, you can use `addTypeTemplate` to perform this operation:
645
663
 
646
664
  ```js
647
- import { defineNuxtModule, addTemplate, addTypeTemplate } from '@nuxt/kit'
665
+ import { addTemplate, addTypeTemplate, defineNuxtModule } from '@nuxt/kit'
648
666
 
649
667
  export default defineNuxtModule({
650
668
  setup (options, nuxt) {
@@ -658,9 +676,9 @@ export default defineNuxtModule({
658
676
  interface NitroRouteRules extends MyModuleNitroRules {}
659
677
  interface NitroRouteConfig extends MyModuleNitroRules {}
660
678
  }
661
- export {}`
679
+ export {}`,
662
680
  })
663
- }
681
+ },
664
682
  })
665
683
  ```
666
684
 
@@ -678,7 +696,7 @@ nuxt.hook('prepare:types', ({ references }) => {
678
696
  If you need to update your templates/virtual files, you can leverage the `updateTemplates` utility like this :
679
697
 
680
698
  ```ts
681
- nuxt.hook('builder:watch', async (event, path) => {
699
+ nuxt.hook('builder:watch', (event, path) => {
682
700
  if (path.includes('my-module-feature.config')) {
683
701
  // This will reload the template that you registered
684
702
  updateTemplates({ filter: t => t.filename === 'my-module-feature.mjs' })
@@ -700,7 +718,7 @@ We're still discussing and exploring how to ease unit and integration testing on
700
718
 
701
719
  #### End to End
702
720
 
703
- [Nuxt Test Utils](/docs/getting-started/testing) is the go-to library to help you test your module in an end-to-end way. Here's the workflow to adopt with it:
721
+ [Nuxt Test Utils](/docs/4.x/getting-started/testing) is the go-to library to help you test your module in an end-to-end way. Here's the workflow to adopt with it:
704
722
 
705
723
  1. Create a Nuxt application to be used as a "fixture" inside `test/fixtures/*`
706
724
  2. Setup Nuxt with this fixture inside your test file
@@ -710,24 +728,24 @@ We're still discussing and exploring how to ease unit and integration testing on
710
728
 
711
729
  In practice, the fixture:
712
730
 
713
- ```js [test/fixtures/ssr/nuxt.config.ts]
731
+ ```ts [test/fixtures/ssr/nuxt.config.ts]
714
732
  // 1. Create a Nuxt application to be used as a "fixture"
715
733
  import MyModule from '../../../src/module'
716
734
 
717
735
  export default defineNuxtConfig({
718
736
  ssr: true,
719
737
  modules: [
720
- MyModule
721
- ]
738
+ MyModule,
739
+ ],
722
740
  })
723
741
  ```
724
742
 
725
743
  And its test:
726
744
 
727
- ```js [test/rendering.ts]
728
- import { describe, it, expect } from 'vitest'
745
+ ```ts [test/rendering.ts]
746
+ import { describe, expect, it } from 'vitest'
729
747
  import { fileURLToPath } from 'node:url'
730
- import { setup, $fetch } from '@nuxt/test-utils/e2e'
748
+ import { $fetch, setup } from '@nuxt/test-utils/e2e'
731
749
 
732
750
  describe('ssr', async () => {
733
751
  // 2. Setup Nuxt with this fixture inside your test file
@@ -754,7 +772,7 @@ An example of such a workflow is available on [the module starter](https://githu
754
772
 
755
773
  #### Manual QA With Playground and Externally
756
774
 
757
- Having a playground Nuxt application to test your module when developing it is really useful. [The module starter integrates one for that purpose](#how-to-develop).
775
+ Having a playground Nuxt application to test your module when developing it is really useful. [The module starter integrates one for that purpose](/docs/4.x/guide/going-further/modules#how-to-develop).
758
776
 
759
777
  You can test your module with other Nuxt applications (applications that are not part of your module repository) locally. To do so, you can use [`npm pack`](https://docs.npmjs.com/cli/commands/npm-pack) command, or your package manager equivalent, to create a tarball from your module. Then in your test project, you can add your module to `package.json` packages as: `"my-module": "file:/path/to/tarball.tgz"`.
760
778
 
@@ -810,7 +828,7 @@ export default defineNuxtModule({
810
828
  })
811
829
  ```
812
830
 
813
- This pattern prevents unnecessary work on every build and provides a better developer experience. See the [lifecycle hooks documentation](/docs/api/kit/modules#using-lifecycle-hooks-for-module-installation-and-upgrade) for more details.
831
+ This pattern prevents unnecessary work on every build and provides a better developer experience. See the [lifecycle hooks documentation](/docs/4.x/api/kit/modules#using-lifecycle-hooks-for-module-installation-and-upgrade) for more details.
814
832
 
815
833
  #### Be TypeScript Friendly
816
834
 
@@ -820,7 +838,7 @@ Exposing types and using TypeScript to develop modules benefits users even when
820
838
 
821
839
  #### Avoid CommonJS Syntax
822
840
 
823
- Nuxt relies on native ESM. Please read [Native ES Modules](/docs/guide/concepts/esm) for more information.
841
+ Nuxt relies on native ESM. Please read [Native ES Modules](/docs/4.x/guide/concepts/esm) for more information.
824
842
 
825
843
  #### Document Module Usage
826
844
 
@@ -3,9 +3,9 @@ title: "Nuxt Kit"
3
3
  description: "@nuxt/kit provides features for module authors."
4
4
  ---
5
5
 
6
- Nuxt Kit provides composable utilities to make interacting with [Nuxt Hooks](/docs/api/advanced/hooks), the [Nuxt Interface](/docs/guide/going-further/internals#the-nuxt-interface) and developing [Nuxt Modules](/docs/guide/going-further/modules) super easy.
6
+ Nuxt Kit provides composable utilities to make interacting with [Nuxt Hooks](/docs/4.x/api/advanced/hooks), the [Nuxt Interface](/docs/4.x/guide/going-further/internals#the-nuxt-interface) and developing [Nuxt Modules](/docs/4.x/guide/going-further/modules) super easy.
7
7
 
8
- ::read-more{to="/docs/api/kit"}
8
+ ::read-more{to="/docs/4.x/api/kit"}
9
9
  Discover all Nuxt Kit utilities.
10
10
  ::
11
11
 
@@ -29,22 +29,22 @@ You can install the latest Nuxt Kit by adding it to the `dependencies` section o
29
29
 
30
30
  ### Import Kit Utilities
31
31
 
32
- ```js [test.mjs]
32
+ ```ts [test.mjs]
33
33
  import { useNuxt } from '@nuxt/kit'
34
34
  ```
35
35
 
36
- :read-more{to="/docs/api/kit"}
36
+ :read-more{to="/docs/4.x/api/kit"}
37
37
 
38
38
  ::note
39
39
  Nuxt Kit utilities are only available for modules and not meant to be imported in runtime (components, Vue composables, pages, plugins, or server routes).
40
40
  ::
41
41
 
42
- Nuxt Kit is an [esm-only package](/docs/guide/concepts/esm) meaning that you **cannot** `require('@nuxt/kit')`. As a workaround, use dynamic import in the CommonJS context:
42
+ Nuxt Kit is an [esm-only package](/docs/4.x/guide/concepts/esm) meaning that you **cannot** `require('@nuxt/kit')`. As a workaround, use dynamic import in the CommonJS context:
43
43
 
44
- ```js [test.cjs]
44
+ ```ts [test.cjs]
45
45
  // This does NOT work!
46
46
  // const kit = require('@nuxt/kit')
47
- async function main() {
47
+ async function main () {
48
48
  const kit = await import('@nuxt/kit')
49
49
  }
50
50
  main()