CodeBlanks Logo Icon CodeBlanks

Import meta

JavaScript course · Advanced · Lesson 4/100

Learn to get module metadata with import.meta.

What is import.meta?

import.meta is an object holding information about the module itself. It works only inside ES modules; in regular scripts it throws.

Its most used property is import.meta.url, which returns the module's URL. So a module can know its own location.

For example, when loading a data file from the same folder, you can base the path on this address.

Using import.meta

To get the module's address, write import.meta.url and assign the result to a variable.

const url = import.meta.url;
console.log(url);

In the browser this gives the full URL; in Node it gives a file path. It is useful to see where a module was loaded from.

Exercise Get and log the module address

Get the current module's URL with import.meta.url and assign it to a variable named url. Then log the value of url with console.log.

Show example solution
Assign import.meta.url to a url variable, then log it.
Example solution:
const url = import.meta.url;
console.log(url);

Test yourself

  1. What does import.meta.url return?

    • the module's content
    • the module's size
    • the module's URL
    • the module's name
    Show answer

    the module's URL — Correct! import.meta.url gives the module's address.

  2. Log the module address

    console.log(____.url);
    Show answer
    console.log(import.meta.url);
  3. Match the concepts

    import.meta
    module info
    import.meta.url
    module address
    this
    points to the instance
    module
    CommonJS object

All lessons in Advanced