2021-10-11 12:57:54 +00:00
---
icon: IconDirectory
title: 'plugins'
2022-08-13 07:27:04 +00:00
head.title: Plugins Directory
2021-10-11 12:57:54 +00:00
---
2022-08-13 07:27:04 +00:00
# Plugins Directory
2021-06-08 21:44:30 +00:00
2021-11-21 12:31:44 +00:00
Nuxt will automatically read the files in your `plugins` directory and load them. You can use `.server` or `.client` suffix in the file name to load a plugin only on the server or client side.
2021-10-14 17:31:30 +00:00
::alert{type=warning}
All plugins in your `plugins/` directory are auto-registered, so you should not add them to your `nuxt.config` separately.
::
2022-08-13 07:27:04 +00:00
## Which Files Are Registered
2022-01-18 16:43:41 +00:00
Only files at the top level of the `plugins/` directory (or index files within any subdirectories) will be registered as plugins.
For example:
```bash
plugins
| - myPlugin.ts
| - myOtherPlugin
| --- supportingFile.ts
| --- componentToRegister.vue
| --- index.ts
```
Only `myPlugin.ts` and `myOtherPlugin/index.ts` would be registered.
2022-08-13 07:27:04 +00:00
## Creating Plugins
2021-10-14 17:31:30 +00:00
2022-04-06 05:56:08 +00:00
The only argument passed to a plugin is [`nuxtApp` ](/api/composables/use-nuxt-app ).
2021-10-14 17:31:30 +00:00
```ts
export default defineNuxtPlugin(nuxtApp => {
// Doing something with nuxtApp
})
```
2022-08-13 07:27:04 +00:00
## Automatically Providing Helpers
2021-10-14 17:31:30 +00:00
2022-03-16 11:16:05 +00:00
If you would like to provide a helper on the `NuxtApp` instance, return it from the plugin under a `provide` key. For example:
2021-10-14 17:31:30 +00:00
```ts
2021-11-18 13:11:34 +00:00
export default defineNuxtPlugin(() => {
return {
provide: {
2022-06-10 14:28:17 +00:00
hello: (msg: string) => `Hello ${msg}!`
2021-11-18 13:11:34 +00:00
}
}
2021-10-14 17:31:30 +00:00
})
2021-11-18 13:11:34 +00:00
```
In another file you can use this:
```vue
< template >
< div >
2022-06-10 14:28:17 +00:00
{{ $hello('world') }}
2021-11-18 13:11:34 +00:00
< / div >
< / template >
< script setup lang = "ts" >
// alternatively, you can also use it here
const { $hello } = useNuxtApp()
< / script >
```
2022-08-13 07:27:04 +00:00
## Typing Plugins
2021-10-14 17:31:30 +00:00
2021-11-18 13:11:34 +00:00
If you return your helpers from the plugin, they will be typed automatically; you'll find them typed for the return of `useNuxtApp()` and within your templates.
::alert
2021-11-21 12:31:44 +00:00
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.
2021-11-18 13:11:34 +00:00
::
### Advanced
For advanced use-cases, you can declare the type of injected properties like this:
```ts [index.d.ts]
2021-10-14 17:31:30 +00:00
declare module '#app' {
interface NuxtApp {
$hello (msg: string): string
}
}
2021-11-18 13:11:34 +00:00
declare module '@vue/runtime-core' {
interface ComponentCustomProperties {
$hello (msg: string): string
}
}
export { }
2021-10-14 17:31:30 +00:00
```
2021-10-20 13:33:33 +00:00
2022-08-13 07:27:04 +00:00
## Vue Plugins
2021-10-20 13:33:33 +00:00
2021-11-21 12:31:44 +00:00
If you want to use Vue plugins, like [vue-gtag ](https://github.com/MatteoGabriele/vue-gtag ) to add Google Analytics tags, you can use a Nuxt plugin to do so.
2021-10-20 13:33:33 +00:00
> There is an Open RFC to make this even easier! See [nuxt/framework#1175](https://github.com/nuxt/framework/discussions/1175)
2022-04-16 13:53:36 +00:00
First, install the plugin you want.
2021-10-20 13:33:33 +00:00
```bash
2021-10-21 09:58:07 +00:00
yarn add --dev vue-gtag-next
2021-10-20 13:33:33 +00:00
```
Then create a plugin file `plugins/vue-gtag.client.js` .
```ts
2021-11-11 13:58:08 +00:00
import VueGtag from 'vue-gtag-next'
2021-10-20 13:33:33 +00:00
export default defineNuxtPlugin((nuxtApp) => {
2021-10-22 10:11:37 +00:00
nuxtApp.vueApp.use(VueGtag, {
2021-10-21 09:58:07 +00:00
property: {
2021-11-11 13:58:08 +00:00
id: 'GA_MEASUREMENT_ID'
2021-10-21 09:58:07 +00:00
}
2021-11-11 13:58:08 +00:00
})
})
2021-10-20 13:33:33 +00:00
```
2022-04-09 09:25:13 +00:00
2022-08-13 07:27:04 +00:00
## Vue Directives
2022-07-06 19:10:29 +00:00
Similarly, you can register a custom Vue directive in a plugin. For example, in `plugins/directive.ts` :
```ts
export default defineNuxtPlugin((nuxtApp) => {
nuxtApp.vueApp.directive('focus', {
mounted (el) {
el.focus()
},
getSSRProps (binding, vnode) {
// you can provide SSR-specific props here
return {}
}
})
})
```
:ReadMore{link="https://vuejs.org/guide/reusability/custom-directives.html"}
2022-04-09 09:25:13 +00:00
:LinkExample{link="/examples/app/plugins"}