When [using pinia alongside Nuxt](../ssr/nuxt.md), you will have to create a [Nuxt plugin](https://nuxtjs.org/docs/2.x/directory-structure/plugins) first. This will give you access to the `pinia` instance:
+```ts
+// plugins/myPiniaPlugin.js
+import { PiniaPluginContext } from 'pinia'
+
+function MyPiniaPlugin({ store }: PiniaPluginContext) {
+ store.$subscribe((mutation) => {
+ // react to store changes
+ console.log(`[🍍 ${mutation.storeId}]: ${mutation.type}.`)
+ })
+
+ // Note this has to be typed if you are using TS
+ return { creationTime: new Date() }
+}
+
+export default defineNuxtPlugin(({ $pinia }) => {
+ $pinia.use(MyPiniaPlugin)
+})
+```
+
+Note the above example is using TypeScript, you have to remove the type annotations `PiniaPluginContext` and `Plugin` as well as their imports if you are using a `.js` file.
+
+### Nuxt.js 2
+
```ts
// plugins/myPiniaPlugin.js
import { PiniaPluginContext } from 'pinia'
export default myPlugin
```
-
-Note the above example is using TypeScript, you have to remove the type annotations `PiniaPluginContext` and `Plugin` as well as their imports if you are using a `.js` file.