2021-10-11 12:57:54 +00:00
# Bridge
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
2021-10-11 21:49:54 +00:00
If you're starting a fresh Nuxt 3 project, please skip this section and go to [Nuxt 3 Installation ](/getting-started/installation ).< br >
2021-10-11 16:37:38 +00:00
Learn more in [Introduction ](/getting-started/introduction ).
::
2021-10-11 12:57:54 +00:00
2021-11-04 11:20:40 +00:00
::alert{type=warning}
Nuxt Bridge provides identical features to Nuxt 3 ([docs](/docs/usage)) but there are some limitations, notably that `useAsyncData` and `useFetch` composables are not available. Please read the rest of this page for details.
::
2021-10-11 21:49:54 +00:00
Bridge is a forward-compatibility layer that allows you to experience many of new Nuxt 3 features by simply installing and enabling a Nuxt module.
2021-10-11 12:57:54 +00:00
2021-10-11 21:49:54 +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
2021-10-29 11:26:01 +00:00
Remove any package lock files (`package-lock.json` and `yarn.lock` ) and use the latest `nuxt-edge` :
2021-10-11 12:57:54 +00:00
2021-10-29 11:26:01 +00:00
```diff [package.json]
2021-10-11 12:57:54 +00:00
- "nuxt": "^2.15.0"
+ "nuxt-edge": "latest"
```
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
2021-10-11 16:37:38 +00:00
```bash [NPM]
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
2021-10-12 19:58:42 +00:00
```bash [NPM]
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
::
2021-10-29 11:26:01 +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
Nuxt 3 introduced the new Nuxt CLI command [`nuxi` ](/getting-started/commands/ ). Update your scripts as follows to leverage the better support from Nuxt Bridge:
```diff
{
"scripts": {
- "dev": "nuxt",
+ "dev": "nuxi dev",
- "build": "nuxt build",
+ "build": "nuxi build",
- "start": "nuxt start",
+ "start": "node .output/server/index.mjs"
}
}
```
2021-10-29 11:26:01 +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
}
}
```
2021-10-29 11:26:01 +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-10-19 08:36:36 +00:00
"start": "node .output/server/index.mjs"
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
2021-10-11 16:37:38 +00:00
You can use static `import` , dynamic `import()` and `export default` instead. Using TypeScript by renaming to `nuxt.config.ts` 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-10-29 11:26:01 +00:00
}
2021-10-13 20:01:50 +00:00
```
2021-11-04 11:20:40 +00:00
## Migrate Composition API
If you were previously using `@vue/composition-api` or `@nuxtjs/composition-api` , please read the [composition api migration guide ](/getting-started/bridge-composition-api ).
2021-10-29 11:26:01 +00:00
## Avoid CommonJS syntax
2021-10-11 12:57:54 +00:00
2021-10-11 22:47:43 +00:00
Nuxt 3 natively supports TypeScript and ECMAScript Modules.
2021-10-11 12:57:54 +00:00
2021-10-29 11:26:01 +00:00
### Update the imports
2021-10-11 22:47:43 +00:00
::code-group
2021-10-29 11:26:01 +00:00
2021-10-11 22:47:43 +00:00
```js [Before]
const lib = require('lib')
```
2021-10-29 11:26:01 +00:00
2021-10-11 22:47:43 +00:00
```js [After]
import lib from 'lib'
// or using code-splitting
const lib = await import('lib').then(e => e.default || e)
```
2021-10-29 11:26:01 +00:00
2021-10-11 22:47:43 +00:00
::
2021-10-29 11:26:01 +00:00
### Update the exports
2021-10-11 22:47:43 +00:00
::code-group
2021-10-29 11:26:01 +00:00
2021-10-11 22:47:43 +00:00
```js [Before]
module.exports = ...
```
2021-10-29 11:26:01 +00:00
2021-10-11 22:47:43 +00:00
```js [After]
export default ...
// or using named export
export const hello = ...
```
2021-10-29 11:26:01 +00:00
2021-10-11 22:47:43 +00:00
::
2021-10-29 11:26:01 +00:00
### Avoid using specific CJS syntax
2021-10-11 22:47:43 +00:00
Avoid the usage of `__dirname` and `__filename` as much as possible.
2021-10-11 12:57:54 +00:00
2021-10-29 11:26:01 +00:00
## Remove incompatible and obsolete modules
2021-10-11 12:57:54 +00:00
2021-10-11 22:47:43 +00:00
- Remove `@nuxt/content` (1.x). A rewrite for nuxt 3 is planned (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
2021-11-04 11:20:40 +00:00
- Remove `@vue/composition-api` from your dependencies ([migration guide](/getting-started/bridge-composition-api)).
- Remove `@nuxtjs/composition-api` from your dependencies (and from your modules in `nuxt.config` ) ([migration guide](/getting-started/bridge-composition-api)).
2021-10-11 12:57:54 +00:00
2021-11-04 11:20:40 +00:00
## Exclude built Nitro folder from git
2021-10-13 19:04:46 +00:00
Add the folder `.output` to the `.gitignore` file.
2021-10-29 11:26:01 +00:00
## Ensure everything goes well
2021-10-11 12:57:54 +00:00
✔️ Try with `nuxt dev` and `nuxt build` (or `nuxt generate` ) to see if everything goes well.
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
2021-11-04 11:20:40 +00:00
## New plugins format (optional)
You can now migrate to the Nuxt 3 plugins API, which is slightly different in format from Nuxt 2.
Plugins now take only one argument (`nuxtApp`). You can find out more in [the docs ](/docs/directory-structure/plugins ).
```js
import { defineNuxtPlugin } from '#app'
export default defineNuxtPlugin(nuxtApp => {
nuxtApp.provide('injected', () => 'my injected function')
// now available on `nuxtApp.$injected`
})
```
## New `useMeta` (optional)
Nuxt Bridge provides a new Nuxt 3 meta API that can be accessed with a new `useMeta` composable.
```vue
< script setup >
import { useMeta } from '#app'
useMeta({
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
}
})
```
This `useMeta` composable uses `@vueuse/head` under the hood (rather than `vue-meta` ) to manipulate your `<head>` .
Accordingly, it is recommended not to use both the native Nuxt 2 `head()` properties as well as `useMeta` , as they may conflict.
For more information on how to use this composable, see [the docs ](/docs/usage/meta-tags#usemeta-composable ).
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!
You can check [bridge/src/module.ts ](https://github.com/nuxt/framework/blob/main/packages/bridge/src/module.ts ) for latest defaults.
```ts [nuxt.config.js|ts]
import { defineNuxtConfig } from '@nuxt/bridge'
export default defineNuxtConfig({
bridge: {
// -- Opt-in features --
// Use Vite as the bundler instead of Webpack 4
// vite: true,
2021-11-04 11:20:40 +00:00
// Enable Nuxt 3 compatible useMeta
// meta: true,
2021-11-02 11:53:11 +00:00
// -- Default features --
// Use legacy server instead of Nitro
// nitro: false,
// Disable nuxt 3 compatible `nuxtApp` interface
// app: false,
// Disable composition API support
// capi: false,
// Do not transpile modules
// transpile: false,
// Disable < script setup > s u p p o r t
// scriptSetup: false,
// Disable composables auto importing
// autoImports: false,
// 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
}
})
```