2022-10-06 09:15:30 +00:00
---
title: "Kit Utilities"
description: Nuxt Kit provides composable utilities to help interacting with Nuxt Hooks and Nuxt Builder.
---
2022-04-06 05:56:08 +00:00
# Kit Utilities
2022-01-27 17:02:42 +00:00
2022-11-16 17:21:08 +00:00
::ReadMore{link="/docs/guide/going-further/kit"}
2022-01-27 17:02:42 +00:00
::
2023-07-28 14:36:01 +00:00
## Modules
2022-01-27 17:02:42 +00:00
2023-01-19 19:37:07 +00:00
[source code ](https://github.com/nuxt/nuxt/blob/main/packages/kit/src/module )
2022-01-27 17:02:42 +00:00
2023-07-28 14:46:02 +00:00
### `installModule`
2023-07-28 14:51:17 +00:00
Install specified Nuxt module programmatically. This is helpful when your module depends on other modules. You can pass the module options as an object to `inlineOptions` and they will be passed to the module's `setup` function.
2023-07-28 14:46:02 +00:00
#### Type
```ts
async function installModule (moduleToInstall: string | NuxtModule, inlineOptions?: any, nuxt?: Nuxt)
```
2023-07-28 13:00:44 +00:00
2023-07-28 15:00:45 +00:00
#### Parameters
##### `moduleToInstall`
**Type**: `string` | `NuxtModule`
**Required**: `true`
The module to install. Can be either a string with the module name or a module object itself.
##### `inlineOptions`
**Type**: `any`
**Default**: `{}`
An object with the module options to be passed to the module's `setup` function.
##### `nuxt`
**Type**: `Nuxt`
**Default**: `useNuxt()`
Nuxt instance. If not provided, it will be retrieved from the context via `useNuxt()` call.
2023-07-28 14:46:02 +00:00
#### Examples
2023-07-28 14:50:59 +00:00
2023-07-28 13:00:44 +00:00
```ts
import { defineNuxtModule, installModule } from '@nuxt/kit'
export default defineNuxtModule< ModuleOptions > ({
async setup (options, nuxt) {
// will install @nuxtjs/fontaine with Roboto font and Impact fallback
await installModule('@nuxtjs/fontaine', {
// module configuration
fonts: [
{
family: 'Roboto',
fallbacks: ['Impact'],
fallbackName: 'fallback-a',
}
]
})
}
})
```
2022-01-27 17:02:42 +00:00
2023-07-28 14:36:01 +00:00
## Programmatic Usage
2022-01-27 17:02:42 +00:00
2023-01-19 19:37:07 +00:00
[source code ](https://github.com/nuxt/nuxt/blob/main/packages/kit/src/loader )
2022-01-27 17:02:42 +00:00
2023-07-29 14:45:00 +00:00
Programmatic usage can be helpfull when you want to use Nuxt programmatically, for example, when building a [CLI tool ](https://github.com/nuxt/cli ) or [test utils ](https://github.com/nuxt/nuxt/tree/main/packages/test-utils ).
2023-07-29 12:29:15 +00:00
### `loadNuxt`
2023-07-29 14:21:05 +00:00
Load Nuxt programmatically. It will load the Nuxt configuration, instantiate and return the promise with Nuxt instance.
2023-07-29 12:29:15 +00:00
#### Type
```ts
async function loadNuxt (loadOptions?: LoadNuxtOptions): Promise< Nuxt >
interface LoadNuxtOptions extends LoadNuxtConfigOptions {
dev?: boolean
ready?: boolean
rootDir?: string
config?: LoadNuxtConfigOptions['overrides']
}
```
#### Parameters
##### `loadOptions`
**Type**: `LoadNuxtOptions`
**Default**: `{}`
2023-07-29 14:37:14 +00:00
Loading conditions for Nuxt. `loadNuxt` uses [`c12` ](https://github.com/unjs/c12 ) under the hood, so it accepts the same options as `c12.loadConfig` with some additional options:
2023-07-29 12:29:15 +00:00
- `dev` (optional)
**Type** : `boolean`
**Default** : `false`
If set to `true` , Nuxt will be loaded in development mode.
- `ready` (optional)
**Type** : `boolean`
**Default** : `true`
If set to `true` , Nuxt will be ready to use after the `loadNuxt` call. If set to `false` , you will need to call `nuxt.ready()` to make sure Nuxt is ready to use.
- `rootDir` (optional)
**Type** : `string`
**Default** : `null`
**Deprecated** : Use `cwd` option instead.
The root directory of the Nuxt project.
- `config` (optional)
**Type** : `LoadNuxtConfigOptions['overrides']`
**Default** : `{}`
**Deprecated** : Use `overrides` option instead.
Overrides for the Nuxt configuration.
2023-07-29 14:15:42 +00:00
### `buildNuxt`
2023-07-29 14:24:40 +00:00
Build Nuxt programmatically. It will invoke the builder (currently [@nuxt/vite-builder ](https://github.com/nuxt/nuxt/tree/main/packages/vite ) or [@nuxt/webpack-builder ](https://github.com/nuxt/nuxt/tree/main/packages/webpack )) to bundle the application.
2023-07-29 14:15:42 +00:00
#### Type
```ts
async function buildNuxt (nuxt: Nuxt): Promise< any >
```
#### Parameters
##### `nuxt`
**Type**: `Nuxt`
**Required**: `true`
Nuxt instance to build. It can be retrieved from the context via `useNuxt()` call.
2023-07-29 12:29:15 +00:00
2023-07-29 14:36:39 +00:00
### `loadNuxtConfig`
Load Nuxt configuration. It will return the promise with the configuration object.
#### Type
```ts
async function loadNuxtConfig (opts: LoadNuxtConfigOptions): Promise< NuxtOptions >
```
#### Parameters
##### `opts`
**Type**: `LoadNuxtConfigOptions`
**Required**: `true`
Options to pass in [`c12` ](https://github.com/unjs/c12#options ) `loadConfig` call.
2022-01-27 17:02:42 +00:00
2023-07-28 14:36:01 +00:00
## Compatibility
2022-01-27 17:02:42 +00:00
2023-01-19 19:37:07 +00:00
[source code ](https://github.com/nuxt/nuxt/blob/main/packages/kit/src/compatibility.ts )
2022-01-27 17:02:42 +00:00
- `checkNuxtCompatibility(constraints)`
- `assertNuxtCompatibility(constraints)`
- `hasNuxtCompatibility(constraints)`
- `isNuxt2()`
- `isNuxt3()`
- `getNuxtVersion()`
2023-07-28 14:36:01 +00:00
## Auto-imports
2022-08-22 13:32:34 +00:00
2023-01-19 19:37:07 +00:00
[source code ](https://github.com/nuxt/nuxt/blob/main/packages/kit/src/imports.ts )
2022-08-22 13:32:34 +00:00
2022-09-15 11:20:53 +00:00
- `addImports(imports)`
2023-04-29 22:21:45 +00:00
- `addImportsDir(importDirs, { prepend? })`
2022-11-16 02:26:35 +00:00
- `addImportsSources(importSources)`
2022-08-22 13:32:34 +00:00
2023-07-28 14:36:01 +00:00
## Components
2022-01-27 17:02:42 +00:00
2023-01-19 19:37:07 +00:00
[source code ](https://github.com/nuxt/nuxt/blob/main/packages/kit/src/components.ts )
2022-01-27 17:02:42 +00:00
- `addComponentsDir(dir)`
- `addComponent(componentObject)`
2023-07-28 14:36:01 +00:00
## Context
2022-01-27 17:02:42 +00:00
2023-01-19 19:37:07 +00:00
[source code ](https://github.com/nuxt/nuxt/blob/main/packages/kit/src/context.ts )
2022-01-27 17:02:42 +00:00
- `useNuxt()`
2023-07-28 14:36:01 +00:00
## Pages
2022-11-10 08:41:34 +00:00
2023-01-19 19:37:07 +00:00
[source code ](https://github.com/nuxt/nuxt/blob/main/packages/kit/src/pages.ts )
2022-11-10 08:41:34 +00:00
- `extendPages (callback: pages => void)`
2023-01-21 16:54:02 +00:00
- `extendRouteRules (route: string, rule: NitroRouteConfig, options: ExtendRouteRulesOptions)`
2023-02-06 23:24:56 +00:00
- `addRouteMiddleware (input: NuxtMiddleware | NuxtMiddleware[], options: AddRouteMiddlewareOptions)`
2022-11-10 08:41:34 +00:00
2023-07-28 14:36:01 +00:00
## Plugins
2022-01-27 17:02:42 +00:00
2023-01-19 19:37:07 +00:00
[source code ](https://github.com/nuxt/nuxt/blob/main/packages/kit/src/plugin.ts )
2022-01-27 17:02:42 +00:00
- `addPlugin(pluginOptions, { append? })`
- `addPluginTemplate(pluginOptions, { append? })`
2023-07-28 14:36:01 +00:00
## Templates
2022-01-27 17:02:42 +00:00
2023-01-19 19:37:07 +00:00
[source code ](https://github.com/nuxt/nuxt/blob/main/packages/kit/src/template.ts )
2022-01-27 17:02:42 +00:00
- `addTemplate(templateOptions)`
2023-06-09 21:24:03 +00:00
- `addTypeTemplate(templateOptions)`
2022-10-24 08:53:02 +00:00
- `updateTemplates({ filter?: ResolvedNuxtTemplate => boolean })`
2022-01-27 17:02:42 +00:00
2023-07-28 14:36:01 +00:00
## Nitro
2022-01-27 17:02:42 +00:00
2023-01-19 19:37:07 +00:00
[source code ](https://github.com/nuxt/nuxt/blob/main/packages/kit/src/nitro.ts )
2022-01-27 17:02:42 +00:00
2022-05-06 13:31:52 +00:00
- `addServerHandler (handler)`
- `addDevServerHandler (handler)`
2022-09-15 16:10:50 +00:00
- `useNitro()` (only usable after `ready` hook)
2022-11-03 21:03:12 +00:00
- `addServerPlugin`
- `addPrerenderRoutes`
2022-01-27 17:02:42 +00:00
2023-07-28 14:36:01 +00:00
## Resolving
2022-01-27 17:02:42 +00:00
2023-01-19 19:37:07 +00:00
[source code ](https://github.com/nuxt/nuxt/blob/main/packages/kit/src/resolve.ts )
2022-01-27 17:02:42 +00:00
- `resolvePath (path, resolveOptions?)`
2022-02-07 21:00:20 +00:00
- `resolveAlias (path, aliases?)`
- `findPath (paths, resolveOptions?)`
2022-02-07 21:39:31 +00:00
- `createResolver (base)`
2022-01-27 17:02:42 +00:00
2023-07-28 14:36:01 +00:00
## Logging
2022-08-22 13:32:34 +00:00
2023-01-19 19:37:07 +00:00
[source code ](https://github.com/nuxt/nuxt/blob/main/packages/kit/src/logger.ts )
2022-08-22 13:32:34 +00:00
- `useLogger(scope?)`
2023-07-28 14:36:01 +00:00
## Builder
2022-01-27 17:02:42 +00:00
2023-01-19 19:37:07 +00:00
[source code ](https://github.com/nuxt/nuxt/blob/main/packages/kit/src/build.ts )
2022-01-27 17:02:42 +00:00
- `extendWebpackConfig(callback, options?)`
- `extendViteConfig(callback, options?)`
- `addWebpackPlugin(webpackPlugin, options?)`
- `addVitePlugin(vitePlugin, options?)`
2023-07-30 09:17:36 +00:00
## Examples
### Accessing Nuxt Vite Config
If you are building an integration that needs access to the runtime Vite or webpack config that Nuxt uses, it is possible to extract this using Kit utilities.
Some examples of projects doing this already:
- [histoire ](https://github.com/histoire-dev/histoire/blob/main/packages/histoire-plugin-nuxt/src/index.ts )
- [nuxt-vitest ](https://github.com/danielroe/nuxt-vitest/blob/main/packages/nuxt-vitest/src/config.ts )
- [@storybook-vue/nuxt ](https://github.com/storybook-vue/nuxt/blob/main/src/preset.ts )
Here is a brief example of how you might access the Vite config from a project; you could implement a similar approach to get the webpack configuration.
```js
import { loadNuxt, buildNuxt } from '@nuxt/kit'
// https://github.com/nuxt/nuxt/issues/14534
async function getViteConfig() {
const nuxt = await loadNuxt({ cwd: process.cwd(), dev: false, overrides: { ssr: false } })
return new Promise((resolve, reject) => {
nuxt.hook('vite:extendConfig', (config, { isClient }) => {
if (isClient) {
resolve(config)
throw new Error('_stop_')
}
})
buildNuxt(nuxt).catch((err) => {
if (!err.toString().includes('_stop_')) {
reject(err)
}
})
}).finally(() => nuxt.close())
}
const viteConfig = await getViteConfig()
console.log(viteConfig)
```