app/plugins/ directory and loads them at the creation of the Vue application.
All plugins inside are auto-registered, you don’t need to add them to your
nuxt.config separately.You can use
.server or .client suffix in the file name to load a plugin only on the server or client side.Registered Plugins
Only files at the top level of the directory (or index files within any subdirectories) will be auto-registered as plugins.foo.ts and bar/index.ts would be registered.
To add plugins in subdirectories, you can use the app/plugins option in nuxt.config.ts:
nuxt.config.ts
Creating Plugins
The only argument passed to a plugin isnuxtApp.
plugins/hello.ts
Object Syntax Plugins
It is also possible to define a plugin using an object syntax, for more advanced use cases. For example:plugins/hello.ts
If you are using the object-syntax, the properties are statically analyzed to produce a more optimized build. So you should not define them at runtime.
Registration Order
You can control the order in which plugins are registered by prefixing with ‘alphabetical’ numbering to the file names.02.myOtherPlugin.ts will be able to access anything that was injected by 01.myPlugin.ts.
This is useful in situations where you have a plugin that depends on another plugin.
In case you’re new to ‘alphabetical’ numbering, remember that filenames are sorted as strings, not as numeric values. For example,
10.myPlugin.ts would come before 2.myOtherPlugin.ts. This is why the example prefixes single digit numbers with 0.Loading Strategy
Parallel Plugins
By default, Nuxt loads plugins sequentially. You can define a plugin asparallel so Nuxt won’t wait until the end of the plugin’s execution before loading the next plugin.
plugins/my-plugin.ts
Plugins With Dependencies
If a plugin needs to wait for another plugin before it runs, you can add the plugin’s name to thedependsOn array.
plugins/depending-on-my-plugin.ts
Using Composables
You can use composables as well as utils within Nuxt plugins:app/plugins/hello.ts
If a composable depends on another plugin registered later, it might not work.Plugins are called in order sequentially and before everything else. You might use a composable that depends on another plugin which has not been called yet.
If a composable depends on the Vue.js lifecycle, it won’t work.Normally, Vue.js composables are bound to the current component instance while plugins are only bound to
nuxtApp instance.Providing Helpers
If you would like to provide a helper on theNuxtApp instance, return it from the plugin under a provide key.
app/components/Hello.vue
Note that we highly recommend using
composables instead of providing helpers to avoid polluting the global namespace and keep your main bundle entry small.Typing Plugins
If you return your helpers from the plugin, they will be typed automatically; you’ll find them typed for the return ofuseNuxtApp() and within your templates.
If you need to use a provided helper within another plugin, you can call
useNuxtApp() to get the typed version. But in general, this should be avoided unless you are certain of the plugins’ order.index.d.ts
Vue Plugins
If you want to use Vue plugins, like vue-gtag to add Google Analytics tags, you can use a Nuxt plugin to do so. First, install the Vue plugin dependency, then create a plugin file:app/plugins/vue-gtag.client.ts
Vue Directives
Similarly, you can register a custom Vue directive in a plugin.plugins/my-directive.ts