N
Nuxt8mo ago
Edik

Plugins - add third party plugin to Nuxt

Hello, I would like to add fast-json-patch into Nuxt as plugin. I want it to be auto-imported and avaliable globally. I tried to add it in nuxt.config.ts as module, but it doesnt get accepted, because its not an nuxt module I believe. In docs I only find how to create own plugins and add vue-plugins. Any help is appreciated.
6 Replies
Omnislash
Omnislash8mo ago
Hello, Never tried to add it in the nuxt conf after installation You can instantiate it in a util or composables that will indeed be auto imported Not sure it's the best way ^^, but working so far
Edik
EdikOP8mo ago
Hi, thank you for your response. Using 'util' or 'composable' doesn't seem to be the intended method for adding plugins, does it?
Omnislash
Omnislash8mo ago
Composables is basically for reusable script and is auto imported. For exemple I have one that import zod and prepare a bunch of zobject for me, that I just check with my SchemaValidator. Sooo it's a nice way to prepare/reuse stuff
Hollis Wu
Hollis Wu8mo ago
I guess you can do something like:
export default defineNuxtConfig({
// For auto import in nuxt side
imports: {
presets: [
{ package: "fast-json-patch" },
],
},

// For auto import in nitro side
nitro: {
imports: {
presets: [
{ package: "fast-json-patch" },
],
},
},
})
export default defineNuxtConfig({
// For auto import in nuxt side
imports: {
presets: [
{ package: "fast-json-patch" },
],
},

// For auto import in nitro side
nitro: {
imports: {
presets: [
{ package: "fast-json-patch" },
],
},
},
})
Hollis Wu
Hollis Wu8mo ago
That will expose all symbols in the package as auto import. To select symbols, use the from and imports key in unimport https://github.com/unjs/unimport
GitHub
GitHub - unjs/unimport: Unified utils for auto importing APIs in mo...
Unified utils for auto importing APIs in modules. Contribute to unjs/unimport development by creating an account on GitHub.
Edik
EdikOP8mo ago
Thank you. I think unimport or unplugin are the way to go, as these are in nuxt integrated. I will look further into this.
Want results from more Discord servers?
Add your server