@nuxt/docs 4.2.2 → 4.3.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.
- package/1.getting-started/02.installation.md +2 -5
- package/1.getting-started/03.configuration.md +5 -5
- package/1.getting-started/06.styling.md +5 -1
- package/1.getting-started/07.routing.md +1 -1
- package/1.getting-started/08.seo-meta.md +4 -0
- package/1.getting-started/12.error-handling.md +12 -6
- package/1.getting-started/14.layers.md +47 -13
- package/1.getting-started/15.prerendering.md +6 -0
- package/1.getting-started/16.deployment.md +1 -0
- package/1.getting-started/17.testing.md +12 -2
- package/1.getting-started/18.upgrade.md +14 -1
- package/2.directory-structure/1.app/1.components.md +4 -0
- package/2.directory-structure/1.app/1.layouts.md +32 -0
- package/2.directory-structure/1.app/1.pages.md +21 -0
- package/2.directory-structure/1.app/1.plugins.md +3 -0
- package/2.directory-structure/1.app/3.error.md +9 -5
- package/2.directory-structure/1.layers.md +87 -0
- package/2.directory-structure/1.modules.md +12 -2
- package/2.directory-structure/1.node_modules.md +1 -1
- package/2.directory-structure/1.server.md +24 -4
- package/2.directory-structure/2.nuxtrc.md +3 -0
- package/2.directory-structure/3.tsconfig.md +1 -0
- package/2.directory-structure/index.md +4 -0
- package/3.guide/0.index.md +8 -2
- package/3.guide/1.concepts/1.rendering.md +1 -0
- package/3.guide/1.concepts/2.nuxt-lifecycle.md +1 -1
- package/3.guide/1.concepts/5.modules.md +14 -1
- package/3.guide/1.concepts/8.typescript.md +6 -2
- package/3.guide/3.ai/1.mcp.md +22 -0
- package/3.guide/4.modules/.navigation.yml +3 -0
- package/3.guide/4.modules/1.getting-started.md +103 -0
- package/3.guide/4.modules/2.module-anatomy.md +138 -0
- package/3.guide/4.modules/3.recipes-basics.md +330 -0
- package/3.guide/4.modules/4.recipes-advanced.md +243 -0
- package/3.guide/4.modules/5.testing.md +76 -0
- package/3.guide/4.modules/6.best-practices.md +104 -0
- package/3.guide/4.modules/7.ecosystem.md +32 -0
- package/3.guide/4.modules/index.md +36 -0
- package/3.guide/{4.recipes → 5.recipes}/2.vite-plugin.md +4 -0
- package/3.guide/{4.recipes → 5.recipes}/3.custom-usefetch.md +1 -1
- package/3.guide/{4.recipes → 5.recipes}/4.sessions-and-authentication.md +1 -1
- package/3.guide/{5.going-further → 6.going-further}/1.experimental-features.md +15 -0
- package/3.guide/{5.going-further → 6.going-further}/1.internals.md +3 -3
- package/3.guide/{5.going-further → 6.going-further}/10.runtime-config.md +1 -1
- package/3.guide/{5.going-further → 6.going-further}/2.hooks.md +1 -1
- package/3.guide/{5.going-further → 6.going-further}/4.kit.md +1 -1
- package/3.guide/{5.going-further → 6.going-further}/7.layers.md +26 -0
- package/4.api/1.components/1.nuxt-client-fallback.md +4 -0
- package/4.api/1.components/3.nuxt-layout.md +2 -2
- package/4.api/1.components/8.nuxt-island.md +9 -2
- package/4.api/2.composables/use-async-data.md +3 -3
- package/4.api/2.composables/use-error.md +2 -2
- package/4.api/2.composables/use-fetch.md +2 -2
- package/4.api/2.composables/use-head.md +16 -1
- package/4.api/2.composables/use-nuxt-app.md +2 -0
- package/4.api/2.composables/use-state.md +10 -0
- package/4.api/3.utils/create-error.md +6 -6
- package/4.api/3.utils/define-nuxt-route-middleware.md +1 -1
- package/4.api/3.utils/define-page-meta.md +8 -1
- package/4.api/3.utils/set-page-layout.md +36 -0
- package/4.api/3.utils/set-response-status.md +2 -2
- package/4.api/3.utils/show-error.md +3 -3
- package/4.api/4.commands/add.md +1 -1
- package/4.api/5.kit/1.modules.md +15 -2
- package/4.api/5.kit/14.builder.md +14 -2
- package/4.api/6.nuxt-config.md +2 -2
- package/5.community/4.contribution.md +5 -5
- package/5.community/5.framework-contribution.md +1 -1
- package/5.community/6.roadmap.md +1 -1
- package/5.community/7.changelog.md +10 -0
- package/6.bridge/1.overview.md +8 -0
- package/6.bridge/4.plugins-and-middleware.md +1 -1
- package/6.bridge/8.nitro.md +4 -0
- package/7.migration/2.configuration.md +2 -2
- package/7.migration/20.module-authors.md +2 -2
- package/7.migration/7.component-options.md +1 -1
- package/package.json +1 -1
- package/3.guide/5.going-further/3.modules.md +0 -968
- /package/3.guide/{4.recipes → 5.recipes}/.navigation.yml +0 -0
- /package/3.guide/{4.recipes → 5.recipes}/1.custom-routing.md +0 -0
- /package/3.guide/{5.going-further → 6.going-further}/.navigation.yml +0 -0
- /package/3.guide/{5.going-further → 6.going-further}/1.events.md +0 -0
- /package/3.guide/{5.going-further → 6.going-further}/1.features.md +0 -0
- /package/3.guide/{5.going-further → 6.going-further}/11.nightly-release-channel.md +0 -0
- /package/3.guide/{5.going-further → 6.going-further}/6.nuxt-app.md +0 -0
- /package/3.guide/{5.going-further → 6.going-further}/9.debugging.md +0 -0
- /package/3.guide/{5.going-further → 6.going-further}/index.md +0 -0
|
@@ -112,8 +112,6 @@ For example, you can define a custom handler utility that wraps the original han
|
|
|
112
112
|
**Example:**
|
|
113
113
|
|
|
114
114
|
```ts [server/utils/handler.ts]
|
|
115
|
-
import type { EventHandler, EventHandlerRequest } from 'h3'
|
|
116
|
-
|
|
117
115
|
export const defineWrappedResponseHandler = <T extends EventHandlerRequest, D> (
|
|
118
116
|
handler: EventHandler<T, D>,
|
|
119
117
|
): EventHandler<T, D> =>
|
|
@@ -130,6 +128,28 @@ export const defineWrappedResponseHandler = <T extends EventHandlerRequest, D> (
|
|
|
130
128
|
})
|
|
131
129
|
```
|
|
132
130
|
|
|
131
|
+
```ts [server/api/hello.get.ts]
|
|
132
|
+
export default defineWrappedResponseHandler(event => 'hello world')
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
## Server Alias
|
|
136
|
+
|
|
137
|
+
You can use the `#server` alias to import files from anywhere within the `server/` directory, regardless of the importing file's location.
|
|
138
|
+
|
|
139
|
+
```ts [server/api/users/[id]/profile.ts]
|
|
140
|
+
// Instead of relative paths like this:
|
|
141
|
+
// import { formatUser } from '../../../utils/formatUser'
|
|
142
|
+
|
|
143
|
+
// Use the #server alias:
|
|
144
|
+
import { formatUser } from '#server/utils/formatUser'
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
This alias ensures consistent imports across your server code, especially useful in deeply nested route handlers.
|
|
148
|
+
|
|
149
|
+
::note
|
|
150
|
+
The `#server` alias can only be used within the `server/` directory. Importing from `#server` in client code will result in an error.
|
|
151
|
+
::
|
|
152
|
+
|
|
133
153
|
## Server Types
|
|
134
154
|
|
|
135
155
|
Auto-imports and other types are different for the `server/` directory, as it is running in a different context from the `app/` directory.
|
|
@@ -277,8 +297,8 @@ export default defineEventHandler((event) => {
|
|
|
277
297
|
|
|
278
298
|
if (!Number.isInteger(id)) {
|
|
279
299
|
throw createError({
|
|
280
|
-
|
|
281
|
-
|
|
300
|
+
status: 400,
|
|
301
|
+
statusText: 'ID should be an integer',
|
|
282
302
|
})
|
|
283
303
|
}
|
|
284
304
|
return 'All good'
|
|
@@ -23,6 +23,9 @@ devtools.enabled=true
|
|
|
23
23
|
# Add Nuxt modules
|
|
24
24
|
modules[]=@nuxt/image
|
|
25
25
|
modules[]=nuxt-security
|
|
26
|
+
|
|
27
|
+
# Module setups (automatically added by Nuxt)
|
|
28
|
+
setups.@nuxt/test-utils="3.23.0"
|
|
26
29
|
```
|
|
27
30
|
|
|
28
31
|
If present, the properties in the `nuxt.config` file will overwrite the properties in `.nuxtrc` file.
|
|
@@ -42,6 +42,7 @@ Read more about the different type contexts of a Nuxt project here.
|
|
|
42
42
|
You can customize the TypeScript configuration of your Nuxt project for each context (`app`, `shared`, `node`, and `server`) in the `nuxt.config.ts` file.
|
|
43
43
|
<!-- @case-police-ignore tsConfig -->
|
|
44
44
|
```ts twoslash [nuxt.config.ts]
|
|
45
|
+
// @errors: 2353
|
|
45
46
|
export default defineNuxtConfig({
|
|
46
47
|
typescript: {
|
|
47
48
|
// customize tsconfig.app.json
|
|
@@ -54,6 +54,10 @@ The [`content/`](/docs/4.x/directory-structure/content) directory is enabled by
|
|
|
54
54
|
|
|
55
55
|
The [`modules/`](/docs/4.x/directory-structure/modules) directory is the directory that contains the local modules of the Nuxt application. Modules are used to extend the functionality of the Nuxt application.
|
|
56
56
|
|
|
57
|
+
## Layers Directory
|
|
58
|
+
|
|
59
|
+
The [`layers/`](/docs/4.x/directory-structure/layers) directory allows you to organize and share reusable code, components, composables, and configurations. Layers within this directory are automatically registered in your project.
|
|
60
|
+
|
|
57
61
|
## Nuxt Files
|
|
58
62
|
|
|
59
63
|
- [`nuxt.config.ts`](/docs/4.x/directory-structure/nuxt-config) file is the main configuration file for the Nuxt application.
|
package/3.guide/0.index.md
CHANGED
|
@@ -11,12 +11,18 @@ surround: false
|
|
|
11
11
|
Discover the main concepts behind Nuxt, from auto-import, hybrid rendering to its TypeScript support.
|
|
12
12
|
::
|
|
13
13
|
::card{icon="i-lucide-square-check" title="Best Practices" to="/docs/4.x/guide/best-practices"}
|
|
14
|
-
Learn about best practices when developing with Nuxt
|
|
14
|
+
Learn about best practices when developing with Nuxt.
|
|
15
|
+
::
|
|
16
|
+
::card{icon="i-lucide-bot" title="Working with AI" to="/docs/4.x/guide/ai"}
|
|
17
|
+
Integrate AI tools into your Nuxt workflow with MCP Server and LLMs.txt.
|
|
18
|
+
::
|
|
19
|
+
::card{icon="i-lucide-box" title="Module Author Guide" to="/docs/4.x/guide/modules"}
|
|
20
|
+
Learn how to create Nuxt modules to integrate, enhance or extend any Nuxt application.
|
|
15
21
|
::
|
|
16
22
|
::card{icon="i-lucide-book-open" title="Recipes" to="/docs/4.x/guide/recipes"}
|
|
17
23
|
Find solutions to common problems and learn how to implement them in your Nuxt project.
|
|
18
24
|
::
|
|
19
25
|
::card{icon="i-lucide-star" title="Going Further" to="/docs/4.x/guide/going-further"}
|
|
20
|
-
Master Nuxt with advanced concepts like experimental features, hooks,
|
|
26
|
+
Master Nuxt with advanced concepts like experimental features, hooks, and more.
|
|
21
27
|
::
|
|
22
28
|
::
|
|
@@ -131,6 +131,7 @@ The `200.html` and `404.html` might be useful for the hosting provider you are u
|
|
|
131
131
|
When prerendering a client-rendered app, Nuxt will generate `index.html`, `200.html` and `404.html` files by default. However, if you need to prevent any (or all) of these files from being generated in your build, you can use the `'prerender:generate'` hook from [Nitro](/docs/4.x/getting-started/prerendering#prerendergenerate-nitro-hook).
|
|
132
132
|
|
|
133
133
|
```ts twoslash [nuxt.config.ts]
|
|
134
|
+
// @errors: 2353 7006
|
|
134
135
|
export default defineNuxtConfig({
|
|
135
136
|
ssr: false,
|
|
136
137
|
nitro: {
|
|
@@ -54,7 +54,7 @@ After this step, Nuxt calls the [`app:created`](/docs/4.x/api/advanced/hooks#app
|
|
|
54
54
|
After initializing plugins and before executing middleware, Nuxt calls the `validate` method if it is defined in the `definePageMeta` function. The `validate` method, which can be synchronous or asynchronous, is often used to validate dynamic route parameters.
|
|
55
55
|
|
|
56
56
|
- The `validate` function should return `true` if the parameters are valid.
|
|
57
|
-
- If validation fails, it should return `false` or an object containing a `
|
|
57
|
+
- If validation fails, it should return `false` or an object containing a `status` and/or `statusText` to terminate the request.
|
|
58
58
|
|
|
59
59
|
For more information, see the [Route Validation documentation](/docs/4.x/getting-started/routing#route-validation).
|
|
60
60
|
|
|
@@ -41,8 +41,21 @@ export default defineNuxtConfig({
|
|
|
41
41
|
Nuxt modules are now build-time-only, and the `buildModules` property used in Nuxt 2 is deprecated in favor of `modules`.
|
|
42
42
|
::
|
|
43
43
|
|
|
44
|
+
## Disabling Modules
|
|
45
|
+
|
|
46
|
+
You can disable a module by setting its config key to `false` in your Nuxt config. This is particularly useful when you want to disable modules inherited from layers.
|
|
47
|
+
|
|
48
|
+
```ts [nuxt.config.ts]
|
|
49
|
+
export default defineNuxtConfig({
|
|
50
|
+
// Disable `@nuxt/image` module
|
|
51
|
+
image: false,
|
|
52
|
+
})
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
:read-more{to="/docs/4.x/guide/going-further/layers#disabling-modules-from-layers"}
|
|
56
|
+
|
|
44
57
|
## Create a Nuxt Module
|
|
45
58
|
|
|
46
59
|
Everyone has the opportunity to develop modules and we cannot wait to see what you will build.
|
|
47
60
|
|
|
48
|
-
:read-more{to="/docs/4.x/guide/
|
|
61
|
+
:read-more{to="/docs/4.x/guide/modules" title="Module Author Guide"}
|
|
@@ -27,6 +27,10 @@ To enable type-checking at build or development time, install `vue-tsc` and `typ
|
|
|
27
27
|
bun add -D vue-tsc typescript
|
|
28
28
|
```
|
|
29
29
|
|
|
30
|
+
```bash [deno]
|
|
31
|
+
deno add -D npm:vue-tsc npm:typescript
|
|
32
|
+
```
|
|
33
|
+
|
|
30
34
|
::
|
|
31
35
|
|
|
32
36
|
Then, run [`nuxt typecheck`](/docs/4.x/api/commands/typecheck) command to check your types:
|
|
@@ -52,7 +56,7 @@ Nuxt projects rely on auto-generated types to work properly. These types are sto
|
|
|
52
56
|
The generated `tsconfig.json` files inside the [`.nuxt`](/docs/4.x/directory-structure/nuxt) directory include **recommended basic TypeScript configuration** for your project, references to [auto-imports](/docs/4.x/guide/concepts/auto-imports), [API route types](/docs/4.x/guide/concepts/server-engine#typed-api-routes), path aliases like `#imports`, `~/file`, or `#build/file`, and more.
|
|
53
57
|
|
|
54
58
|
::warning
|
|
55
|
-
Nuxt relies on this configuration, and [Nuxt
|
|
59
|
+
Nuxt relies on this configuration, and [Nuxt modules](/docs/4.x/guide/modules) can extend it as well. For this reason, it is not recommended to modify your `tsconfig.json` file directly, as doing so could overwrite important settings. Instead, extend it via `nuxt.config.ts`. [Learn more about extending the configuration here](/docs/4.x/directory-structure/tsconfig).
|
|
56
60
|
::
|
|
57
61
|
|
|
58
62
|
::tip{icon="i-lucide-video" to="https://youtu.be/umLI7SlPygY" target="_blank"}
|
|
@@ -95,7 +99,7 @@ Similarly:
|
|
|
95
99
|
- For the `server` context, place the augmentation file in the `server/` directory.
|
|
96
100
|
- For types that are **shared between the app and server**, place the file in the `shared/` directory.
|
|
97
101
|
|
|
98
|
-
::read-more{to="/docs/4.x/guide/
|
|
102
|
+
::read-more{to="/docs/4.x/guide/modules/recipes-advanced#extend-typescript-config"}
|
|
99
103
|
Read more about augmenting specific type contexts from **files outside those contexts** in the Module Author Guide.
|
|
100
104
|
::
|
|
101
105
|
|
package/3.guide/3.ai/1.mcp.md
CHANGED
|
@@ -88,6 +88,28 @@ Add the server using the CLI command:
|
|
|
88
88
|
claude mcp add --transport http nuxt-remote https://nuxt.com/mcp
|
|
89
89
|
```
|
|
90
90
|
|
|
91
|
+
### Claude Desktop
|
|
92
|
+
|
|
93
|
+
#### Setup Instructions
|
|
94
|
+
|
|
95
|
+
1. Open Claude Desktop and navigate to "Settings" > "Developer".
|
|
96
|
+
2. Click on "Edit Config". This will open the local Claude directory.
|
|
97
|
+
3. Modify the `claude_desktop_config.json` file with your custom MCP server configuration.
|
|
98
|
+
```json [claude_desktop_config.json]
|
|
99
|
+
{
|
|
100
|
+
"mcpServers": {
|
|
101
|
+
"nuxt": {
|
|
102
|
+
"command": "npx",
|
|
103
|
+
"args": [
|
|
104
|
+
"mcp-remote",
|
|
105
|
+
"https://nuxt.com/mcp"
|
|
106
|
+
]
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
```
|
|
111
|
+
4. Restart Claude Desktop app. The Nuxt MCP server should now be registered.
|
|
112
|
+
|
|
91
113
|
### Cursor
|
|
92
114
|
|
|
93
115
|
Click the button below to install the Nuxt MCP server directly in Cursor:
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: "Create Your First Module"
|
|
3
|
+
description: "Learn how to create your first Nuxt module using the official starter template."
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
## Create a Module
|
|
7
|
+
|
|
8
|
+
We recommend you get started with Nuxt modules using our [starter template](https://github.com/nuxt/starter/tree/module):
|
|
9
|
+
|
|
10
|
+
::code-group{sync="pm"}
|
|
11
|
+
|
|
12
|
+
```bash [npm]
|
|
13
|
+
npm create nuxt -- -t module my-module
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
```bash [yarn]
|
|
17
|
+
yarn create nuxt -t module my-module
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
```bash [pnpm]
|
|
21
|
+
pnpm create nuxt -t module my-module
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
```bash [bun]
|
|
25
|
+
bun create nuxt --template=module my-module
|
|
26
|
+
```
|
|
27
|
+
::
|
|
28
|
+
|
|
29
|
+
This will create a `my-module` project with all the boilerplate necessary to develop and publish your module.
|
|
30
|
+
|
|
31
|
+
**Next steps:**
|
|
32
|
+
|
|
33
|
+
1. Open `my-module` in your IDE of choice
|
|
34
|
+
2. Install dependencies using your favorite package manager
|
|
35
|
+
3. Prepare local files for development using `npm run dev:prepare`
|
|
36
|
+
4. Follow this document to learn more about Nuxt modules
|
|
37
|
+
|
|
38
|
+
## Use the Starter Template
|
|
39
|
+
|
|
40
|
+
Learn how to perform basic tasks with the module starter.
|
|
41
|
+
|
|
42
|
+
::tip{icon="i-lucide-video" to="https://vueschool.io/lessons/navigating-the-official-starter-template?friend=nuxt" target="_blank"}
|
|
43
|
+
Watch Vue School video about Nuxt module starter template.
|
|
44
|
+
::
|
|
45
|
+
|
|
46
|
+
### Develop Your Module
|
|
47
|
+
|
|
48
|
+
While your module source code lives inside the `src` directory, to develop a module you often need a Nuxt application to test it against. That's what the `playground` directory is for. It's a Nuxt application you can tinker with that is already configured to run with your module.
|
|
49
|
+
|
|
50
|
+
You can interact with the playground like with any Nuxt application.
|
|
51
|
+
|
|
52
|
+
- Launch its development server with `npm run dev`, it should reload itself as you make changes to your module in the `src` directory
|
|
53
|
+
- Build it with `npm run dev:build`
|
|
54
|
+
|
|
55
|
+
::note
|
|
56
|
+
All other `nuxt` commands can be used against the `playground` directory (e.g. `nuxt <COMMAND> playground`). Feel free to declare additional `dev:*` scripts within your `package.json` referencing them for convenience.
|
|
57
|
+
::
|
|
58
|
+
|
|
59
|
+
### Run Tests
|
|
60
|
+
|
|
61
|
+
The module starter comes with a basic test suite:
|
|
62
|
+
|
|
63
|
+
- A linter powered by [ESLint](https://eslint.org), run it with `npm run lint`
|
|
64
|
+
- A test runner powered by [Vitest](https://vitest.dev), run it with `npm run test` or `npm run test:watch`
|
|
65
|
+
|
|
66
|
+
::tip
|
|
67
|
+
Feel free to augment this default test strategy to better suit your needs.
|
|
68
|
+
::
|
|
69
|
+
|
|
70
|
+
### Build Your Module
|
|
71
|
+
|
|
72
|
+
Nuxt modules come with their own builder provided by [`@nuxt/module-builder`](https://github.com/nuxt/module-builder#readme). This builder doesn't require any configuration on your end, supports TypeScript, and makes sure your assets are properly bundled to be distributed to other Nuxt applications.
|
|
73
|
+
|
|
74
|
+
You can build your module by running `npm run prepack`.
|
|
75
|
+
|
|
76
|
+
::tip
|
|
77
|
+
While building your module can be useful in some cases, most of the time you won't need to build it on your own: the `playground` takes care of it while developing, and the release script also has you covered when publishing.
|
|
78
|
+
::
|
|
79
|
+
|
|
80
|
+
### Publish to npm
|
|
81
|
+
|
|
82
|
+
::important
|
|
83
|
+
Before publishing your module to npm, makes sure you have an [npmjs.com](https://www.npmjs.com) account and that you're authenticated to it locally with `npm login`.
|
|
84
|
+
::
|
|
85
|
+
|
|
86
|
+
While you can publish your module by bumping its version and using the `npm publish` command, the module starter comes with a release script that helps you make sure you publish a working version of your module to npm and more.
|
|
87
|
+
|
|
88
|
+
To use the release script, first, commit all your changes (we recommend you follow [Conventional Commits](https://www.conventionalcommits.org) to also take advantage of automatic version bump and changelog update), then run the release script with `npm run release`.
|
|
89
|
+
|
|
90
|
+
When running the release script, the following will happen:
|
|
91
|
+
|
|
92
|
+
- First, it will run your test suite by:
|
|
93
|
+
- Running the linter (`npm run lint`)
|
|
94
|
+
- Running unit, integration, and e2e tests (`npm run test`)
|
|
95
|
+
- Building the module (`npm run prepack`)
|
|
96
|
+
- Then, if your test suite went well, it will proceed to publish your module by:
|
|
97
|
+
- Bumping your module version and generating a changelog according to your Conventional Commits
|
|
98
|
+
- Publishing the module to npm (for that purpose, the module will be built again to ensure its updated version number is taken into account in the published artifact)
|
|
99
|
+
- Pushing a git tag representing the newly published version to your git remote origin
|
|
100
|
+
|
|
101
|
+
::tip
|
|
102
|
+
As with other scripts, feel free to fine-tune the default `release` script in your `package.json` to better suit your needs.
|
|
103
|
+
::
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: "Understand Module Structure"
|
|
3
|
+
description: "Learn how Nuxt modules are structured and how to define them."
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
There are two types of Nuxt modules:
|
|
7
|
+
|
|
8
|
+
- published modules are distributed on npm - you can see a list of some community modules on [the Nuxt website](/modules).
|
|
9
|
+
- "local" modules exist within a Nuxt project, either [inlined in Nuxt config](/docs/4.x/api/nuxt-config#modules) or within [the `modules` directory](/docs/4.x/directory-structure/modules).
|
|
10
|
+
|
|
11
|
+
In either case, they work in the same way.
|
|
12
|
+
|
|
13
|
+
## Define Your Module
|
|
14
|
+
|
|
15
|
+
::note
|
|
16
|
+
When using the starter, your module definition is available at `src/module.ts`.
|
|
17
|
+
::
|
|
18
|
+
|
|
19
|
+
The module definition is the entry point of your module. It's what gets loaded by Nuxt when your module is referenced within a Nuxt configuration.
|
|
20
|
+
|
|
21
|
+
At a low level, a Nuxt module definition is a simple, potentially asynchronous, function accepting inline user options and a `nuxt` object to interact with Nuxt.
|
|
22
|
+
|
|
23
|
+
```ts
|
|
24
|
+
export default function (inlineOptions, nuxt) {
|
|
25
|
+
// You can do whatever you like here..
|
|
26
|
+
console.log(inlineOptions.token) // `123`
|
|
27
|
+
console.log(nuxt.options.dev) // `true` or `false`
|
|
28
|
+
nuxt.hook('ready', (nuxt) => {
|
|
29
|
+
console.log('Nuxt is ready')
|
|
30
|
+
})
|
|
31
|
+
}
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
You can get type hinting for this function using the higher-level `defineNuxtModule` helper provided by [Nuxt Kit](/docs/4.x/guide/going-further/kit).
|
|
35
|
+
|
|
36
|
+
```ts
|
|
37
|
+
import { defineNuxtModule } from '@nuxt/kit'
|
|
38
|
+
|
|
39
|
+
export default defineNuxtModule((options, nuxt) => {
|
|
40
|
+
nuxt.hook('pages:extend', (pages) => {
|
|
41
|
+
console.log(`Discovered ${pages.length} pages`)
|
|
42
|
+
})
|
|
43
|
+
})
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
However, **we do not recommend** using this low-level function definition. Instead, to define a module, **we recommend** using the object-syntax with `meta` property to identify your module, especially when publishing to npm.
|
|
47
|
+
|
|
48
|
+
This helper makes writing Nuxt modules more straightforward by implementing many common patterns needed by modules, guaranteeing future compatibility and improving the experience for both module authors and users.
|
|
49
|
+
|
|
50
|
+
```ts
|
|
51
|
+
import { defineNuxtModule } from '@nuxt/kit'
|
|
52
|
+
|
|
53
|
+
export default defineNuxtModule({
|
|
54
|
+
meta: {
|
|
55
|
+
// Usually the npm package name of your module
|
|
56
|
+
name: '@nuxtjs/example',
|
|
57
|
+
// The key in `nuxt.config` that holds your module options
|
|
58
|
+
configKey: 'sample',
|
|
59
|
+
// Compatibility constraints
|
|
60
|
+
compatibility: {
|
|
61
|
+
// Semver version of supported nuxt versions
|
|
62
|
+
nuxt: '>=3.0.0',
|
|
63
|
+
},
|
|
64
|
+
},
|
|
65
|
+
// Default configuration options for your module, can also be a function returning those
|
|
66
|
+
defaults: {},
|
|
67
|
+
// Shorthand sugar to register Nuxt hooks
|
|
68
|
+
hooks: {},
|
|
69
|
+
// Configuration for other modules - this does not ensure the module runs before
|
|
70
|
+
// your module, but it allows you to change the other module's configuration before it runs
|
|
71
|
+
moduleDependencies: {
|
|
72
|
+
'some-module': {
|
|
73
|
+
// You can specify a version constraint for the module. If the user has a different
|
|
74
|
+
// version installed, Nuxt will throw an error on startup.
|
|
75
|
+
version: '>=2',
|
|
76
|
+
// By default moduleDependencies will be added to the list of modules to be installed
|
|
77
|
+
// by Nuxt unless `optional` is set.
|
|
78
|
+
optional: true,
|
|
79
|
+
// Any configuration that should override `nuxt.options`.
|
|
80
|
+
overrides: {},
|
|
81
|
+
// Any configuration that should be set. It will override module defaults but
|
|
82
|
+
// will not override any configuration set in `nuxt.options`.
|
|
83
|
+
defaults: {},
|
|
84
|
+
},
|
|
85
|
+
},
|
|
86
|
+
// The function holding your module logic, it can be asynchronous
|
|
87
|
+
setup (moduleOptions, nuxt) {
|
|
88
|
+
// ...
|
|
89
|
+
},
|
|
90
|
+
})
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
`defineNuxtModule` returns a wrapper function with the lower level `(inlineOptions, nuxt)` module signature. This wrapper function applies defaults and other necessary steps before calling your `setup` function:
|
|
94
|
+
|
|
95
|
+
- Support `defaults` and `meta.configKey` for automatically merging module options
|
|
96
|
+
- Type hints and automated type inference
|
|
97
|
+
- Ensure module gets installed only once using a unique key computed from `meta.name` or `meta.configKey`
|
|
98
|
+
- Automatically register Nuxt hooks
|
|
99
|
+
- Automatically check for compatibility issues based on module meta
|
|
100
|
+
- Expose `getOptions` and `getMeta` for internal usage of Nuxt
|
|
101
|
+
- Ensuring backward and upward compatibility as long as the module is using `defineNuxtModule` from the latest version of `@nuxt/kit`
|
|
102
|
+
- Integration with module builder tooling
|
|
103
|
+
|
|
104
|
+
## Add Runtime Code
|
|
105
|
+
|
|
106
|
+
::note
|
|
107
|
+
When using the starter, the runtime directory is `src/runtime/`.
|
|
108
|
+
::
|
|
109
|
+
|
|
110
|
+
Modules, like everything in a Nuxt configuration, aren't included in your application runtime. However, you might want your module to provide, or inject runtime code to the application it's installed on. That's what the runtime directory enables you to do.
|
|
111
|
+
|
|
112
|
+
Inside the runtime directory, you can provide any kind of assets related to the Nuxt app:
|
|
113
|
+
- Vue components
|
|
114
|
+
- Composables
|
|
115
|
+
- [Nuxt plugins](/docs/4.x/directory-structure/app/plugins)
|
|
116
|
+
|
|
117
|
+
To the [server engine](/docs/4.x/guide/concepts/server-engine), Nitro:
|
|
118
|
+
- API routes
|
|
119
|
+
- Middlewares
|
|
120
|
+
- Nitro plugins
|
|
121
|
+
|
|
122
|
+
Or any other kind of asset you want to inject in users' Nuxt applications:
|
|
123
|
+
- Stylesheets
|
|
124
|
+
- 3D models
|
|
125
|
+
- Images
|
|
126
|
+
- etc.
|
|
127
|
+
|
|
128
|
+
You'll then be able to inject all those assets inside the application from your [module definition](#define-your-module).
|
|
129
|
+
|
|
130
|
+
::tip
|
|
131
|
+
Learn more about asset injection in [the recipes section](/docs/4.x/guide/modules/recipes-basics).
|
|
132
|
+
::
|
|
133
|
+
|
|
134
|
+
::warning
|
|
135
|
+
Published modules cannot leverage auto-imports for assets within their runtime directory. Instead, they have to import them explicitly from `#imports` or alike.
|
|
136
|
+
:br :br
|
|
137
|
+
Auto-imports are not enabled for files within `node_modules` (the location where a published module will eventually live) for performance reasons.
|
|
138
|
+
::
|