How to write a nuxt module compatible with Nuxt2 & Nuxt3 ?

I am trying to create a nuxt module that would be compatible with nuxt 2 & nuxt 3.

The only thing the module does is to add a plugin

How could I write that ?

Is something like that allowed and a good idea ?

export default defineNuxtModule({
    // ...
    setup() {
        addPluginTemplate({
              getContents: "export default defineNuxtPlugin(() => console.log('foo'))",
        })
    }
})


I am affraid that defineNuxtPlugin doesn't exists on Nuxt 2, will it work ?
Was this page helpful?