Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: create a page explaining import.meta #24186

Merged
merged 5 commits into from Nov 8, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
49 changes: 49 additions & 0 deletions docs/3.api/6.advanced/2.Import meta.md
@@ -0,0 +1,49 @@
---
title: 'Import meta'
description: 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{to="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/import.meta"}

## Properties

Property | Type | Description
--- | --- | ---
`import.meta.client` | boolean | True when evaluated on the client-side, false otherwise.
`import.meta.dev` | boolean | Equals `nuxt.config.dev`
`import.meta.env` | object | Equals `process.env`
`import.meta.server` | boolean | True when evaluated on the server-side, false otherwise.
`import.meta.url` | string | Resolvable path for the current file.

::callout{color="amber" icon="i-ph-warning-duotone"}
In nuxt modules, only `import.meta.url` is supported.
::

## Examples

### Using `import.meta.url` to resolve files within your nuxt-module

```js
// /modules/my-module/index.ts

// Resolve relative from the current file
const {resolve} = createResolver(import.meta.url)
danielroe marked this conversation as resolved.
Show resolved Hide resolved

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