Nuxt Kit utilities can be used in Nuxt 3, Nuxt 2 with Bridge and even Nuxt 2 without Bridge. To make sure your module is compatible with all versions, you can use the checkNuxtCompatibility, assertNuxtCompatibility and hasNuxtCompatibility functions. They will check if the current Nuxt version meets the constraints you provide. Also you can use isNuxt2, isNuxt3 and getNuxtVersion functions for more granular checks.
checkNuxtCompatibilityChecks if constraints are met for the current Nuxt version. If not, returns an array of messages. Nuxt 2 version also checks for bridge support.
twoslash
export default defineNuxtModule({
async setup (_options, nuxt) {
const issues = await checkNuxtCompatibility({ nuxt: '^2.16.0' }, nuxt)
if (issues.length) {
console.warn('Nuxt compatibility issues found:\n' + issues.toString())
} else {
// do something
}
},
})function checkNuxtCompatibility (constraints: NuxtCompatibility, nuxt?: Nuxt): Promise<NuxtCompatibilityIssues>constraints: Version and builder constraints to check against. It accepts the following properties:
| Property | Type | Required | Description |
|---|---|---|---|
nuxt | string | false | Nuxt version in semver format. Versions may be defined in Node.js way, for example: >=2.15.0 <3.0.0. |
nuxt: Nuxt instance. If not provided, it will be retrieved from the context via useNuxt() call.
assertNuxtCompatibilityAsserts that constraints are met for the current Nuxt version. If not, throws an error with the list of issues as string.
twoslash
// @errors: 2391
// ---cut---
function assertNuxtCompatibility (constraints: NuxtCompatibility, nuxt?: Nuxt): Promise<true>constraints: Version and builder constraints to check against. Refer to the constraints table in checkNuxtCompatibility for details.
nuxt: Nuxt instance. If not provided, it will be retrieved from the context via useNuxt() call.
hasNuxtCompatibilityChecks if constraints are met for the current Nuxt version. Return true if all constraints are met, otherwise returns false. Nuxt 2 version also checks for bridge support.
twoslash
export default defineNuxtModule({
async setup (_options, nuxt) {
const usingNewPostcss = await hasNuxtCompatibility({ nuxt: '^2.16.0' }, nuxt)
if (usingNewPostcss) {
// do something
} else {
// do something else
}
},
})function hasNuxtCompatibility (constraints: NuxtCompatibility, nuxt?: Nuxt): Promise<boolean>constraints: Version and builder constraints to check against. Refer to the constraints table in checkNuxtCompatibility for details.
nuxt: Nuxt instance. If not provided, it will be retrieved from the context via useNuxt() call.
isNuxtMajorVersionCheck if current Nuxt instance is of specified major version
twoslash
export default defineNuxtModule({
setup () {
if (isNuxtMajorVersion(3)) {
// do something for Nuxt 3
} else {
// do something else for other versions
}
},
})function isNuxtMajorVersion (major: number, nuxt?: Nuxt): booleanmajor: Major version to check against.
nuxt: Nuxt instance. If not provided, it will be retrieved from the context via useNuxt() call.
isNuxt3Checks if the current Nuxt version is 3.x.
isNuxtMajorVersion(2, nuxt) instead. This may be removed in \@nuxt/kit v5 or a future major version.function isNuxt3 (nuxt?: Nuxt): booleannuxt: Nuxt instance. If not provided, it will be retrieved from the context via useNuxt() call.
isNuxt2Checks if the current Nuxt version is 2.x.
isNuxtMajorVersion(2, nuxt) instead. This may be removed in \@nuxt/kit v5 or a future major version.function isNuxt2 (nuxt?: Nuxt): booleannuxt: Nuxt instance. If not provided, it will be retrieved from the context via useNuxt() call.
getNuxtVersionReturns the current Nuxt version.
function getNuxtVersion (nuxt?: Nuxt): stringnuxt: Nuxt instance. If not provided, it will be retrieved from the context via useNuxt() call.