Docs Hub

DownloadsLaravelLaravelLivewireLivewireAlpine.jsAlpine.jsNext.jsNext.jsVue.jsVue.jsZustandZustandNuxt.jsNuxt.jsFilamentFilamentVite.jsVite.jsNest.jsNest.jsBootstrapBootstrapTailwindCSSTailwindCSSدرحال بازسازی
ReactReact
راه های ارتباطی با ما

Docs Hub

Nuxt.jsNuxt.js
ModulesProgrammaticCompatibilityAutoimportsComponentsContextPagesLayoutHeadPluginsRuntime ConfigTemplatesNitroResolvingLoggingBuilderExamplesLayers
Nuxt ConfigOverview
Docs Hub

Nuxt modules allow you to enhance Nuxt's capabilities. They offer a structured way to keep your code organized and modular. If you're looking to break down your module into smaller components, Nuxt offers the useNuxt and tryUseNuxt functions. These functions enable you to conveniently access the Nuxt instance from the context without having to pass it as an argument.

📝 When you're working with the setup function in Nuxt modules, Nuxt is already provided as the second argument. This means you can access it directly without needing to call useNuxt().

useNuxt

Get the Nuxt instance from the context. It will throw an error if Nuxt is not available.

Usage

ts

const setupSomeFeature = () => {
  const nuxt = useNuxt()

  // You can now use the nuxt instance
  console.log(nuxt.options)
}

Type

ts
twoslash
// @errors: 2391
// ---cut---
function useNuxt (): Nuxt

Return Value

The useNuxt function returns the Nuxt instance, which contains all the options and methods available in Nuxt.

PropertyTypeDescription
optionsNuxtOptionsThe resolved Nuxt configuration.
hooksHookable<NuxtHooks>The Nuxt hook system. Allows registering and listening to lifecycle events.
callHook(name: string, ...args: any[]) => Promise<any>Shortcut for nuxt.hooks.callHook. Triggers a lifecycle hook manually and runs all registered callbacks.
addHooks(configHooks: NestedHooks) => () => voidShortcut for nuxt.hooks.addHooks. Registers multiple hooks at once.

Examples

setupTranspilation.ts

export const setupTranspilation = () => {
  const nuxt = useNuxt()

  if (nuxt.options.builder === '@nuxt/webpack-builder') {
    nuxt.options.build.transpile ||= []
    nuxt.options.build.transpile.push('xstate')
  }
}

tryUseNuxt

Get the Nuxt instance from the context. It will return null if Nuxt is not available.

Usage

ts
twoslash

function setupSomething () {
  const nuxt = tryUseNuxt()

  if (nuxt) {
    // You can now use the nuxt instance
    console.log(nuxt.options)
  } else {
    console.log('Nuxt is not available')
  }
}

Type

ts
twoslash
// @errors: 2391
// ---cut---
function tryUseNuxt (): Nuxt | null

Return Value

The tryUseNuxt function returns the Nuxt instance if available, or null if Nuxt is not available.

The Nuxt instance as described in the useNuxt section.

Examples

requireSiteConfig.ts
declare module 'nuxt/schema' {
  interface NuxtOptions {
    siteConfig: SiteConfig
  }
}
// ---cut---

interface SiteConfig {
  title?: string
}

export const requireSiteConfig = (): SiteConfig => {
  const nuxt = tryUseNuxt()
  if (!nuxt) {
    return {}
  }
  return nuxt.options.siteConfig
}