2021-10-11 12:57:54 +00:00
# Migration
2021-10-11 16:37:38 +00:00
Nuxt 3 migration guide. Work in progress 🚧
2021-10-11 12:57:54 +00:00
2021-10-11 16:37:38 +00:00
## Nuxt 2 to Nuxt 3
2021-10-11 12:57:54 +00:00
2021-10-12 02:08:26 +00:00
At the moment, there is no Nuxt 2 to Nuxt 3 migration guide nor is it recommended due to potentially more changes coming.
2021-10-11 12:57:54 +00:00
We are working to provide a stable migration guide and tooling to make it as smooth as possible. Please check [Bridge ](/getting-started/bridge ) for the alternative.
2021-10-11 21:49:54 +00:00
Some features have been dropped from Nuxt 2, some are yet to be implemented for Nuxt 3 and some are new in Nuxt 3 (and Bridge).
2021-10-11 12:57:54 +00:00
2021-10-11 21:49:54 +00:00
Noticeable and/or breaking changes for Nuxt 3 other than the requirements of Nuxt Bridge are:
2021-10-11 12:57:54 +00:00
2021-10-11 16:37:38 +00:00
::list{type=warning}
2021-10-29 11:26:01 +00:00
2021-10-11 12:57:54 +00:00
- Vue app templates are rewritten
- Vue upgraded to `3.x`
2021-10-11 16:37:38 +00:00
- Using `<Suspense>` for async data fetching
2021-10-11 12:57:54 +00:00
- Webpack `5.x` (if not using `vite` )
- Components discovery is rewritten
2021-10-11 16:37:38 +00:00
- Introduced main [`app.vue` component ](/docs/directory-structure/app )
- Introduced new [`layouts` system ](/docs/directory-structure/layouts )
- The [`pages/` directory ](/docs/directory-structure/pages ) conventions changed
::
2021-10-11 12:57:54 +00:00
2021-11-21 12:31:44 +00:00
The below table highlights an overall feature comparison:
2021-10-11 12:57:54 +00:00
2022-01-31 10:59:32 +00:00
Feature / Version | Nuxt 2 | Nuxt 3 | Changes required | More information
--------------------------|---------|----------|---------------------------------|------------------
Vue Version | 2 | 3 | Yes |
`app.vue` | ❌ | ✅ | - |
Suspense | ❌ | ✅ | - |
Assets | ✅ | ✅ | No |
Components | ✅ | ✅ | No |
Layouts | ✅ | ✅ | Yes |
Middleware | ✅ | ✅ | Yes |
Pages | ✅ | ✅ | Yes |
Pages: Dynamic Params | ✅ | ✅ | Yes |
Pages: _.vue | ✅ | ✅ | Yes (new naming system) |
Plugins | ✅ | ✅ | Yes (compatible by default) |
Transitions | ✅ | ✅ | ? |
Options API: `asyncData` | ✅ | ✅ | Yes (use `defineNuxtComponent` ) |
Options API: `fetch` | ✅ | ❌ | Use `asyncData` instead |
Error Pages | ✅ | 🚧 | Yes | [Handling runtime errors ](https://github.com/nuxt/framework/discussions/559 )
Store | ✅ | 🚧 | Yes | [Global store support ](https://github.com/nuxt/framework/discussions/571 )
Static builds | ✅ | 🚧 | | [Route Caching rules (Hybrid) ](https://github.com/nuxt/framework/discussions/560 )
2021-10-11 12:57:54 +00:00
2021-10-11 16:37:38 +00:00
### Module Compatibility
2021-10-11 12:57:54 +00:00
2021-10-11 16:37:38 +00:00
All Nuxt 2 modules should be forward compatible with Nuxt 3 as long as they migrate to bridge or if they are already following guidelines.
2021-10-11 12:57:54 +00:00
2021-10-11 16:37:38 +00:00
All (upcoming) modules made with `@nuxt/kit` should be backward compatible with Nuxt 2 projects (even without bridge) as long as they are not depending on a Nuxt 3 / Bridge-only feature.
2021-10-11 12:57:54 +00:00
2021-10-11 16:37:38 +00:00
### Plugin Compatibility
Most Nuxt 2 plugins should be forward compatible with Nuxt 3 with a magical compat layer we inject.
Nuxt 3 plugins are **not** backward compatible with Nuxt 2.
2021-10-11 12:57:54 +00:00
### Vue Compatibility
2021-10-11 16:37:38 +00:00
For plugins using composition API or components, it needs exclusive Vue 2 or Vue 3 support.
By using [vue-demi ](https://github.com/vueuse/vue-demi ) they should be compatible with both Nuxt 2 and 3.
## Module Migration
2021-10-11 21:49:54 +00:00
When Nuxt 3 users add your module, a compatible module container layer from `@nuxt/kit` is **automatically injected** , so as long as your code is following the guidelines below, it should continue working as-is.
2021-10-11 16:37:38 +00:00
### Test with `@nuxt/bridge`
Migrating to `@nuxt/bridge` is the first and most important step for supporting Nuxt 3.
If you have a fixture or example in your module, add `@nuxt/bridge` package to its config (see [example ](/getting-started/bridge#update-nuxtconfig ))
2021-11-04 23:24:06 +00:00
### Migrate from CommonJS to ESM
2021-10-11 16:37:38 +00:00
2021-11-04 23:24:06 +00:00
Nuxt 3 natively supports TypeScript and ECMAScript Modules. Please check [Native ES Modules ](/concepts/esm ) for more info and upgrading.
2021-10-11 12:57:54 +00:00
2021-10-11 16:37:38 +00:00
### Ensure plugins default export
2021-10-11 12:57:54 +00:00
2021-10-18 15:07:17 +00:00
If you inject a Nuxt plugin that does not have `export default` (such as global Vue plugins), ensure you add `export default () => { }` to the end of it.
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
```js [Before]
// ~/plugins/vuelidate.js
import Vue from 'vue'
import Vuelidate from 'vuelidate'
2021-10-11 12:57:54 +00:00
2021-10-11 16:37:38 +00:00
Vue.use(Vuelidate)
```
2021-10-29 11:26:01 +00:00
2021-10-11 16:37:38 +00:00
```js [After]
// ~/plugins/vuelidate.js
import Vue from 'vue'
import Vuelidate from 'vuelidate'
2021-10-11 12:57:54 +00:00
2021-10-11 16:37:38 +00:00
Vue.use(Vuelidate)
2021-10-11 12:57:54 +00:00
2021-10-13 09:07:03 +00:00
export default () => { }
2021-10-11 16:37:38 +00:00
```
2021-10-29 11:26:01 +00:00
2021-10-11 16:37:38 +00:00
::
2021-10-11 12:57:54 +00:00
### Avoid runtime modules
2021-10-11 21:49:54 +00:00
With Nuxt 3, Nuxt is now a build-time-only dependency, which means that modules shouldn't attempt to hook into the Nuxt runtime.
2021-10-11 12:57:54 +00:00
2021-10-11 21:49:54 +00:00
Your module needs should work even if it's only added to [`buildModules` ](/docs/directory-structure/nuxt.config#buildmodules ) (instead of `modules` ). For example:
2021-10-11 12:57:54 +00:00
2021-10-11 21:49:54 +00:00
- Avoid updating `process.env` within a Nuxt module and reading by a nuxt plugin; use [`runtimeConfig` ](/docs/directory-structure/nuxt.config#publicruntimeconfig ) instead.
2021-10-11 12:57:54 +00:00
- (*) Avoid depending on runtime hooks like `vue-renderer:*` for production
2021-11-21 12:31:44 +00:00
- (*) Avoid adding `serverMiddleware` by importing them inside the module. Instead, add them by referencing a file path so that they are independent of the module's context
2021-10-11 12:57:54 +00:00
(*) Unless it is for `nuxt dev` purpose only and guarded with `if (nuxt.options.dev) { }` .
### Add module meta
Ensure your module is exporting meta object.
2021-10-11 16:37:38 +00:00
\[TODO\]
2021-10-11 12:57:54 +00:00
2021-10-11 16:37:38 +00:00
### Use TypeScript (optional)
2021-10-11 12:57:54 +00:00
2021-10-11 21:49:54 +00:00
While it is not essential, most of the Nuxt ecosystem is shifting to use TypeScript, so it is highly recommended to consider migration.
2021-10-11 12:57:54 +00:00
2021-10-11 16:37:38 +00:00
::alert{icon=💡}
You can start migration by simply renaming `.js` files, to `.ts` . TypeScript is designed to be progressive!
::
2021-10-11 12:57:54 +00:00
2021-10-11 16:37:38 +00:00
::alert{icon=💡}
You can use TypeScript syntax for Nuxt 2 and 3 modules and plugins without any extra dependencies.
::