2022-10-06 09:15:30 +00:00
# Overview
2021-10-11 12:57:54 +00:00
2021-10-11 16:37:38 +00:00
Experience Nuxt 3 features on existing Nuxt 2 projects.
2021-10-11 12:57:54 +00:00
2021-10-11 16:37:38 +00:00
::alert
2022-11-16 10:04:28 +00:00
If you're starting a fresh Nuxt 3 project, please skip this section and go to [Nuxt 3 Installation ](/docs/getting-started/introduction ).
2021-10-11 16:37:38 +00:00
::
2021-10-11 12:57:54 +00:00
2021-11-04 11:20:40 +00:00
::alert{type=warning}
2023-07-07 16:24:09 +00:00
Nuxt Bridge provides identical features to Nuxt 3 ([docs](/docs/guide/concepts/auto-imports)) but there are some limitations, notably that [`useAsyncData` ](/docs/api/composables/use-async-data ) and [`useFetch` ](/docs/api/composables/use-fetch ) composables are not available. Please read the rest of this page for details.
2021-11-04 11:20:40 +00:00
::
2021-11-21 12:31:44 +00:00
Bridge is a forward-compatibility layer that allows you to experience many of the new Nuxt 3 features by simply installing and enabling a Nuxt module.
2021-10-11 12:57:54 +00:00
2023-06-05 15:03:06 +00:00
Using Nuxt Bridge, you can make sure your project is (almost) ready for Nuxt 3 and have the best developer experience without needing a major rewrite or risk breaking changes.
2021-10-11 12:57:54 +00:00
2021-10-29 11:26:01 +00:00
## Upgrade Nuxt 2
2021-10-11 16:37:38 +00:00
2023-02-06 22:41:31 +00:00
Make sure your dev server (`nuxt dev`) isn't running, remove any package lock files (`package-lock.json` and `yarn.lock` ), and install the latest Nuxt 2 version:
2021-10-11 12:57:54 +00:00
2021-10-29 11:26:01 +00:00
```diff [package.json]
2023-06-22 11:36:07 +00:00
- "nuxt": "^2.16.3"
+ "nuxt": "^2.17.0"
2021-10-11 12:57:54 +00:00
```
2021-10-11 21:49:54 +00:00
Then, reinstall your dependencies:
2021-10-11 12:57:54 +00:00
2021-10-11 16:37:38 +00:00
::code-group
2021-10-29 11:26:01 +00:00
2021-10-11 16:37:38 +00:00
```bash [Yarn]
yarn install
```
2021-10-29 11:26:01 +00:00
2022-12-05 10:44:28 +00:00
```bash [npm]
2021-10-11 16:37:38 +00:00
npm install
```
2021-10-29 11:26:01 +00:00
2021-10-11 16:37:38 +00:00
::
2021-10-11 12:57:54 +00:00
2021-10-11 16:37:38 +00:00
::alert
2021-10-11 21:49:54 +00:00
Once the installation is complete, make sure both development and production builds are working as expected before proceeding.
2021-10-11 16:37:38 +00:00
::
2021-10-11 12:57:54 +00:00
2021-10-29 11:26:01 +00:00
## Install Nuxt Bridge
2021-10-11 12:57:54 +00:00
2021-10-11 16:37:38 +00:00
Install `@nuxt/bridge-edge` as a development dependency:
::code-group
2021-10-29 11:26:01 +00:00
2021-10-11 16:37:38 +00:00
```bash [Yarn]
2021-10-11 12:57:54 +00:00
yarn add --dev @nuxt/bridge@npm:@nuxt/bridge -edge
2021-10-12 19:58:42 +00:00
```
2021-10-29 11:26:01 +00:00
2022-12-05 10:44:28 +00:00
```bash [npm]
2021-10-12 19:58:42 +00:00
npm install -D @nuxt/bridge@npm:@nuxt/bridge -edge
2021-10-11 12:57:54 +00:00
```
2021-10-29 11:26:01 +00:00
2021-10-11 16:37:38 +00:00
::
2022-08-13 07:27:04 +00:00
## Update Your Scripts
2021-10-18 13:54:55 +00:00
You will also need to update your scripts within your `package.json` to reflect the fact that Nuxt will now produce a Nitro server as build output.
2021-10-29 11:26:01 +00:00
### Nuxi
2021-10-29 10:38:22 +00:00
2022-11-16 10:04:28 +00:00
Nuxt 3 introduced the new Nuxt CLI command [`nuxi` ](/docs/api/commands/add ). Update your scripts as follows to leverage the better support from Nuxt Bridge:
2021-10-29 10:38:22 +00:00
```diff
{
"scripts": {
- "dev": "nuxt",
+ "dev": "nuxi dev",
- "build": "nuxt build",
+ "build": "nuxi build",
- "start": "nuxt start",
2021-12-23 19:27:08 +00:00
+ "start": "nuxi preview"
2021-10-29 10:38:22 +00:00
}
}
```
2023-06-15 17:44:13 +00:00
::alert
If `nitro: false` , use the `nuxt` command.
::
2022-08-13 07:27:04 +00:00
### Static Target
2021-10-18 13:54:55 +00:00
2021-10-29 10:38:22 +00:00
If you have set `target: 'static'` in your `nuxt.config` then you need to ensure that you update your build script to be `nuxi generate` .
2021-10-18 13:54:55 +00:00
2021-10-29 11:26:01 +00:00
```json [package.json]
2021-10-18 13:54:55 +00:00
{
"scripts": {
2021-10-29 10:38:22 +00:00
"build": "nuxi generate"
2021-10-18 13:54:55 +00:00
}
}
```
2022-08-13 07:27:04 +00:00
### Server Target
2021-10-18 13:54:55 +00:00
2021-10-29 10:38:22 +00:00
For all other situations, you can use the `nuxi build` command.
2021-10-18 13:54:55 +00:00
2021-10-29 11:26:01 +00:00
```json [package.json]
2021-10-18 13:54:55 +00:00
{
"scripts": {
2021-10-29 10:38:22 +00:00
"build": "nuxi build",
2021-12-23 19:27:08 +00:00
"start": "nuxi preview"
2021-10-18 13:54:55 +00:00
}
}
```
2021-10-29 11:26:01 +00:00
## Update `nuxt.config`
2021-10-11 12:57:54 +00:00
2021-10-11 21:49:54 +00:00
Please make sure to avoid any CommonJS syntax such as `module.exports` , `require` or `require.resolve` in your config file. It will soon be deprecated and unsupported.
2021-10-11 12:57:54 +00:00
2023-07-07 16:24:09 +00:00
You can use static `import` , dynamic `import()` and `export default` instead. Using TypeScript by renaming to [`nuxt.config.ts` ](/docs/guide/directory-structure/nuxt.config ) is also possible and recommended.
2021-10-11 12:57:54 +00:00
2021-10-13 08:58:28 +00:00
```ts [nuxt.config.js|ts]
2021-10-11 12:57:54 +00:00
import { defineNuxtConfig } from '@nuxt/bridge'
export default defineNuxtConfig({
// Your existing configuration
})
```
2021-10-29 11:26:01 +00:00
## Update `tsconfig.json`
2021-10-13 20:01:50 +00:00
2021-10-29 11:26:01 +00:00
If you are using TypeScript, you can edit your `tsconfig.json` to benefit from auto-generated Nuxt types:
2021-10-13 20:01:50 +00:00
2021-10-29 11:26:01 +00:00
```diff [tsconfig.json]
2021-10-13 20:01:50 +00:00
{
2021-11-02 11:53:11 +00:00
+ "extends": "./.nuxt/tsconfig.json",
"compilerOptions": {
...
2021-11-29 11:13:48 +00:00
}
2021-10-29 11:26:01 +00:00
}
2021-10-13 20:01:50 +00:00
```
2022-07-19 11:35:27 +00:00
::alert
As `.nuxt/tsconfig.json` is generated and not checked into version control, you'll need to generate that file before running your tests. Add `nuxi prepare` as a step before your tests, otherwise you'll see `TS5083: Cannot read file '~/.nuxt/tsconfig.json'`
::
2021-11-15 15:58:43 +00:00
::alert
2022-10-21 08:08:48 +00:00
You may also need to add `@vue/runtime-dom` as a devDependency if you are struggling to get template type inference working with [Volar ](https://marketplace.visualstudio.com/items?itemName=Vue.volar ).
2021-11-15 15:58:43 +00:00
::
2021-12-21 13:01:31 +00:00
::alert
Keep in mind that all options extended from `./.nuxt/tsconfig.json` will be overwritten by the options defined in your `tsconfig.json` .
2022-03-31 13:45:43 +00:00
Overwriting options such as `"compilerOptions.paths"` with your own configuration will lead TypeScript to not factor in the module resolutions from `./.nuxt/tsconfig.json` . This can lead to module resolutions such as `#imports` not being recognized.
2021-12-21 13:01:31 +00:00
2023-07-13 15:25:54 +00:00
In case you need to extend options provided by `./.nuxt/tsconfig.json` further, you can use the `alias` property within your `nuxt.config` . `nuxi` will pick them up and extend `./.nuxt/tsconfig.json` accordingly.
2021-12-21 13:01:31 +00:00
::
2021-11-15 15:58:43 +00:00
2022-09-26 09:37:31 +00:00
## Update Runtime Config
Nuxt 3 approaches runtime config differently than Nuxt 2, using a new combined `runtimeConfig` option.
First, you'll need to combine your `publicRuntimeConfig` and `privateRuntimeConfig` properties into a new one called `runtimeConfig` , with the public config within a key called `public` .
```diff
// nuxt.config.js
- privateRuntimeConfig: {
- apiKey: process.env.NUXT_API_KEY || 'super-secret-key'
- },
- publicRuntimeConfig: {
- websiteURL: 'https://public-data.com'
- }
+ runtimeConfig: {
+ apiKey: process.env.NUXT_API_KEY || 'super-secret-key',
+ public: {
+ websiteURL: 'https://public-data.com'
+ }
+ }
```
This also means that when you need to access public runtime config, it's behind a property called `public` . If you use public runtime config, you'll need to update your code.
```diff
// MyWidget.vue
- < div > Website: {{ $config.websiteURL }}</ div >
+ < div > Website: {{ $config.public.websiteURL }}< / div >
```
2021-11-04 11:20:40 +00:00
## Migrate Composition API
2022-11-16 16:35:05 +00:00
If you were using `@vue/composition-api` or `@nuxtjs/composition-api` , please read the [composition api migration guide ](/docs/bridge/bridge-composition-api ).
2021-11-04 11:20:40 +00:00
2021-11-04 23:24:06 +00:00
### Migrate from CommonJS to ESM
2021-10-11 12:57:54 +00:00
2023-02-02 16:09:55 +00:00
Nuxt 3 natively supports TypeScript and ECMAScript Modules. Please check [Native ES Modules ](/docs/guide/concepts/esm ) for more info and upgrading.
2021-10-11 12:57:54 +00:00
2022-08-13 07:27:04 +00:00
## Remove Incompatible and Obsolete Modules
2021-10-11 12:57:54 +00:00
2022-11-17 07:26:54 +00:00
- Remove `@nuxt/content` (1.x). A rewrite for Nuxt 3 [is available ](https://content.nuxtjs.org/ ) (2.x)
2021-10-11 12:57:54 +00:00
- Remove `nuxt-vite` : Bridge enables same functionality
2021-10-13 08:56:59 +00:00
- Remove `@nuxt/typescript-build` : Bridge enables same functionality
- Remove `@nuxt/typescript-runtime` and `nuxt-ts` : Nuxt 2 has built-in runtime support
2021-10-11 12:57:54 +00:00
- Remove `@nuxt/nitro` : Bridge injects same functionality
2022-11-16 16:35:05 +00:00
- Remove `@vue/composition-api` from your dependencies ([migration guide](/docs/bridge/bridge-composition-api)).
- Remove `@nuxtjs/composition-api` from your dependencies (and from your modules in `nuxt.config` ) ([migration guide](/docs/bridge/bridge-composition-api)).
2021-10-11 12:57:54 +00:00
2022-08-13 07:27:04 +00:00
## Exclude Built Nitro Folder From Git
2021-10-13 19:04:46 +00:00
Add the folder `.output` to the `.gitignore` file.
2022-08-13 07:27:04 +00:00
## Ensure Everything Goes Well
2021-10-11 12:57:54 +00:00
2021-11-21 12:32:28 +00:00
✔️ Try with `nuxi dev` and `nuxi build` (or `nuxi generate` ) to see if everything goes well.
2021-10-11 12:57:54 +00:00
2021-11-02 11:53:11 +00:00
🐛 Is something wrong? Please let us know by creating an issue. Also, you can easily disable the bridge in the meantime:
2021-10-11 12:57:54 +00:00
2021-10-13 08:58:28 +00:00
```ts [nuxt.config.js|ts]
2021-10-11 12:57:54 +00:00
import { defineNuxtConfig } from '@nuxt/bridge'
export default defineNuxtConfig({
2021-10-11 21:49:54 +00:00
bridge: false // Temporarily disable bridge integration
2021-10-11 12:57:54 +00:00
})
```
2021-11-02 11:53:11 +00:00
2022-08-13 07:27:04 +00:00
## New Plugins Format (Optional)
2021-11-04 11:20:40 +00:00
You can now migrate to the Nuxt 3 plugins API, which is slightly different in format from Nuxt 2.
2022-11-16 10:04:28 +00:00
Plugins now take only one argument (`nuxtApp`). You can find out more in [the docs ](/docs/guide/directory-structure/plugins ).
2021-11-04 11:20:40 +00:00
```js
export default defineNuxtPlugin(nuxtApp => {
nuxtApp.provide('injected', () => 'my injected function')
// now available on `nuxtApp.$injected`
})
```
2021-11-29 10:50:49 +00:00
::alert
2023-07-07 16:24:09 +00:00
If you want to use the new Nuxt composables (such as [`useNuxtApp` ](/docs/api/composables/use-nuxt-app ) or `useRuntimeConfig` ) within your plugins, you will need to use the `defineNuxtPlugin` helper for those plugins.
2021-11-29 10:50:49 +00:00
::
2022-03-25 11:53:56 +00:00
::alert{type=warning}
Although a compatibility interface is provided via `nuxtApp.vueApp` you should avoid registering plugins, directives, mixins or components this way without adding your own logic to ensure they are not installed more than once, or this may cause a memory leak.
::
2023-06-23 14:47:21 +00:00
## New Middleware Format (Optional)
You can now migrate to the Nuxt 3 middleware API, which is slightly different in format from Nuxt 2.
Middleware now take only two argument (`to`, `from` ). You can find out more in [the docs ](/docs/guide/directory-structure/middleware ).
```js
export default defineNuxtRouteMiddleware((to) => {
if (to.path !== '/') {
return navigateTo('/')
}
})
```
::alert{type=warning}
Use of `defineNuxtRouteMiddleware` is not supported outside of the middleware directory.
::
::alert{type=warning}
Nuxt Bridge does not support `definePageMeta` .
::
2022-08-13 07:27:04 +00:00
## New `useHead` (Optional)
2021-11-04 11:20:40 +00:00
2023-07-07 16:24:09 +00:00
Nuxt Bridge provides a new Nuxt 3 meta API that can be accessed with a new [`useHead` ](/docs/api/composables/use-head ) composable.
2021-11-04 11:20:40 +00:00
```vue
2023-07-18 10:31:45 +00:00
< script setup lang = "ts" >
2022-04-05 14:02:29 +00:00
useHead({
2021-11-04 11:20:40 +00:00
title: 'My Nuxt App',
})
< / script >
```
You will also need to enable this feature explicitly in your `nuxt.config` :
```js
import { defineNuxtConfig } from '@nuxt/bridge'
export default defineNuxtConfig({
bridge: {
meta: true
}
})
```
2022-10-26 09:18:22 +00:00
::alert
2023-07-07 16:24:09 +00:00
This [`useHead` ](/docs/api/composables/use-head ) composable uses `@vueuse/head` under the hood (rather than `vue-meta` ) to manipulate your `<head>` . You need to add `@vueuse/head` to your package.json file for it to work properly.
2022-10-26 09:18:22 +00:00
::
::alert{type=warning}
2023-07-07 16:24:09 +00:00
We recommend not using the native Nuxt 2 `head()` properties in addition to [`useHead` ](/docs/api/composables/use-head ) , as they may conflict.
2022-10-26 09:18:22 +00:00
::
2021-11-04 11:20:40 +00:00
2022-11-16 10:04:28 +00:00
For more information on how to use this composable, see [the docs ](/docs/getting-started/seo-meta ).
2021-11-04 11:20:40 +00:00
2021-11-02 11:53:11 +00:00
## Feature Flags
You can optionally disable some features from bridge or opt-in to less stable ones. In normal circumstances, it is always best to stick with defaults!
2023-02-10 16:32:40 +00:00
You can check [bridge/src/module.ts ](https://github.com/nuxt/bridge/blob/main/packages/bridge/src/module.ts ) for latest defaults.
2021-11-02 11:53:11 +00:00
```ts [nuxt.config.js|ts]
import { defineNuxtConfig } from '@nuxt/bridge'
export default defineNuxtConfig({
bridge: {
// -- Opt-in features --
2022-03-30 17:32:30 +00:00
// Use Vite as the bundler instead of webpack 4
2021-11-02 11:53:11 +00:00
// vite: true,
2022-04-05 14:02:29 +00:00
// Enable Nuxt 3 compatible useHead
2021-11-04 11:20:40 +00:00
// meta: true,
2021-11-02 11:53:11 +00:00
// -- Default features --
// Use legacy server instead of Nitro
// nitro: false,
2022-08-13 07:27:04 +00:00
// Disable Nuxt 3 compatible `nuxtApp` interface
2021-11-02 11:53:11 +00:00
// app: false,
2022-04-04 10:56:10 +00:00
// Disable Composition API support
2021-11-02 11:53:11 +00:00
// capi: false,
2022-04-04 10:56:10 +00:00
// ... or just disable legacy Composition API support
2021-12-17 09:44:59 +00:00
// capi: {
// legacy: false
// },
2021-11-02 11:53:11 +00:00
// Do not transpile modules
// transpile: false,
// Disable < script setup > s u p p o r t
// scriptSetup: false,
// Disable composables auto importing
2022-08-23 14:22:11 +00:00
// imports: false,
2021-11-02 11:53:11 +00:00
// Do not warn about module incompatibilities
2021-11-04 11:20:40 +00:00
// constraints: false
2021-11-02 11:53:11 +00:00
},
vite: {
// Config for Vite
}
})
```