Movatterモバイル変換


[0]ホーム

URL:


MDN Web Docs

import.meta

BaselineWidely available *

Theimport.meta meta-property exposes context-specific metadata to a JavaScript module. It contains information about the module, such as the module's URL.

Syntax

js
import.meta

Value

Theimport.meta object is created by the host environment, as an extensiblenull-prototype object where all properties are writable, configurable, and enumerable. The spec doesn't specify any properties to be defined on it, but hosts usually implement the following properties:

url

The full URL to the module, includes query parameters and/or hash (following the? or#). In browsers, this is either the URL from which the script was obtained (for external scripts), or the URL of the containing document (for inline scripts). In Node.js, this is the file path (including thefile:// protocol).

resolve

Resolves a module specifier to a URL using the current module's URL as base.

Description

Theimport.meta syntax consists of the keywordimport, a dot, and the identifiermeta. Becauseimport is areserved word, not an identifier, this is not aproperty accessor, but a special expression syntax.

Theimport.meta meta-property is available in JavaScript modules; usingimport.meta outside of a module (includingdirecteval() within a module) is a syntax error.

Examples

Passing query parameters

Using query parameters in theimport specifier allows module-specific argument passing, which may be complementary to reading parameters from the application-widewindow.location (or on Node.js, throughprocess.argv). For example, with the following HTML:

html
<script type="module">  import "./index.mjs?someURLInfo=5";</script>

Theindex.mjs module is able to retrieve thesomeURLInfo parameter throughimport.meta:

js
// index.mjsnew URL(import.meta.url).searchParams.get("someURLInfo"); // 5

The same applies when a module imports another:

js
// index.mjsimport "./index2.mjs?someURLInfo=5";// index2.mjsnew URL(import.meta.url).searchParams.get("someURLInfo"); // 5

The ES module implementation in Node.js supports resolving module specifiers containing query parameters (or the hash), as in the latter example. However, you cannot use queries or hashes when the module is specified through the CLI command (likenode index.mjs?someURLInfo=5), because the CLI entrypoint uses a more CommonJS-like resolution mode, treating the path as a file path rather than a URL. To pass parameters to the entrypoint module, use CLI arguments and read them throughprocess.argv instead (likenode index.mjs --someURLInfo=5).

Resolving a file relative to the current one

In Node.js CommonJS modules, there's a__dirname variable that contains the absolute path to the folder containing current module, which is useful for resolving relative paths. However, ES modules cannot have contextual variables except forimport.meta. Therefore, to resolve a relative file you can useimport.meta.url. Note that this uses URLs rather than filesystem paths.

Before (CommonJS):

js
const fs = require("fs/promises");const path = require("path");const filePath = path.join(__dirname, "someFile.txt");fs.readFile(filePath, "utf8").then(console.log);

After (ES modules):

js
import fs from "node:fs/promises";const fileURL = new URL("./someFile.txt", import.meta.url);fs.readFile(fileURL, "utf8").then(console.log);

Specifications

Specification
ECMAScript® 2026 Language Specification
# prod-ImportMeta

Browser compatibility

See also

Help improve MDN

Learn how to contribute.

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp