The official Nuxt Certification Program in partnership with VueSchool is coming.

Import meta

Understand where your code is running using `import.meta`.

The import.meta object

With ES modules you can obtain some metadata from the code that imports or compiles your ES-module. This is done through import.meta, which is an object that provides your code with this information. Throughout the Nuxt documentation you may see snippets that use this already to figure out whether the code is currently running on the client or server side.

Read more in https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/import.meta.

Runtime (App) Properties

These values are statically injected and can be used for tree-shaking your runtime code.

PropertyTypeDescription
import.meta.clientbooleanTrue when evaluated on the client side.
import.meta.browserbooleanTrue when evaluated on the client side.
import.meta.serverbooleanTrue when evaluated on the server side.
import.meta.nitrobooleanTrue when evaluated on the server side.
import.meta.devbooleanTrue when running the Nuxt dev server.
import.meta.testbooleanTrue when running in a test context.
import.meta.prerenderbooleanTrue when rendering HTML on the server in the prerender stage of your build.

Builder Properties

These values are available both in modules and in your nuxt.config.

PropertyTypeDescription
import.meta.envobjectEquals process.env
import.meta.urlstringResolvable path for the current file.

Examples

Using import.meta.url to resolve files within modules

modules/my-module/index.ts
import { createResolver } from 'nuxt/kit'

// Resolve relative from the current file
const resolver = createResolver(import.meta.url)

export default defineNuxtModule({
  meta: { name: 'myModule' },
  setup() {
    addComponent({
      name: 'MyModuleComponent',
      // Resolves to '/modules/my-module/components/MyModuleComponent.vue'
      filePath: resolver.resolve('./components/MyModuleComponent.vue')
    })
  }
})