Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Next.js and React

The browser refuses an import that has no ./ or URL in front

Your error

Uncaught TypeError: Failed to resolve module specifier "vue". Relative references must start with either "/", "./", or "../".1 more line it can show

Fix it fast

Most common cause, 1 of 4

A local file is imported without ./ or /

Prefix every import of your own modules with ./ or ../ (relative to the importing file) or with / (relative to the site root), and keep the .js extension, because the browser fetches the exact URL.

Fix
// before
import { formatDate } from "utils.js";
// after
import { formatDate } from "./utils.js";
Did this fix it?
Full steps for this cause: check, fix, verify

Short answer

TypeError: Failed to resolve module specifier "x". Relative references must start with either "/", "./", or "../" means a browser module imported a bare name it cannot turn into a URL. Write local files as ./file.js, add a <script type="importmap"> above your module scripts for package names, or build with a bundler. Import maps do not reach module workers.

  • Applies to Chrome 158 stable (Chromium error text), HTML import maps: imports, scopes, multiple maps
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Which cause do you have?

Read the name inside the quotes of the error, then list the module scripts and import maps the page loads, in order. A name that is one of your own files points to cause 1, a package name with no import map to cause 2, a map that exists but misses the name or comes later to cause 3, and a stack that starts in a worker file to cause 4.

Run this first
grep -nE 'type="(module|importmap)"' index.html
# in the DevTools console:
[...document.scripts].map(s => s.type + ' ' + (s.src || '(inline)'))

Then find the line you see in this list and open its fix:

  1. 1A local file is imported without ./ or /The quoted name is a path from your own project, such as utils.js or src/api.js
  2. 2An npm package name is used directly in the browserThe quoted name is a package such as vue, three or lodash-es, and the page has no type="importmap" script
  3. 3The import map misses a subpath or loads too lateImporting the package root works but a path under it fails (for example three/addons/...), or the importmap tag sits below a module script
  4. 4The bare import runs inside a module workerThe page's own imports work, but the failing stack points at a file started with new Worker(url, { type: "module" })

What does the error look like?

  • Uncaught TypeError: Failed to resolve module specifier "vue". Relative references must start with either "/", "./", or "../".
  • TypeError: Failed to resolve module specifier 'lodash-es'

Cause 1 of 4

A local file is imported without ./ or /

You seeThe quoted name is a path from your own project, such as utils.js or src/api.js

Check

Chrome throws Failed to resolve module specifier when an import in your own code names a file without a leading ./, ../ or /. The browser reads utils.js as a bare name rather than a path, and with nothing mapping it the resolver rejects it.

Check
grep -rnE "from ['\"][a-zA-Z@][^'\"]*\.js['\"]" --include=*.js . | grep -v "://"

Fix

Prefix every import of your own modules with ./ or ../ (relative to the importing file) or with / (relative to the site root), and keep the .js extension, because the browser fetches the exact URL.

Fix
// before
import { formatDate } from "utils.js";
// after
import { formatDate } from "./utils.js";

Verify

Reload with the cache disabled. The Network tab shows a 200 for the module and the TypeError is gone.

Verify
grep -rn 'from "utils.js"' --include=*.js . || echo "no bare local imports left"

Confirmed by raw.githubusercontent.com

Did this fix it?

Cause 2 of 4

An npm package name is used directly in the browser

You seeThe quoted name is a package such as vue, three or lodash-es, and the page has no type="importmap" script

Check

Node and bundlers follow their own rules for package names. A page served as plain files only has the browser's rules, so the name must become an absolute URL, a relative path, or a key in an import map.

Check
grep -c 'type="importmap"' index.html

Fix

Add an import map before the first module script and point each package name at a URL that serves its ES module build. The other route, when the project already installs packages with npm, is a build step with a bundler, so the shipped files no longer contain bare names.

Fix
<script type="importmap">
  {
    "imports": {
      "my-lib": "/vendor/my-lib/index.js"
    }
  }
</script>
<script type="module" src="/main.js"></script>

Verify

The import of my-lib loads and the console shows no resolution error.

Verify
document.querySelectorAll('script[type="importmap"]').length

Confirmed by developer.mozilla.org

Did this fix it?

Cause 3 of 4

The import map misses a subpath or loads too late

You seeImporting the package root works but a path under it fails (for example three/addons/...), or the importmap tag sits below a module script

Check

A package name key only matches that exact name. Imports of files inside the package need a second key that ends in a slash, mapped to a URL that also ends in a slash. Separately, a map is only used for modules loaded after it is processed, so a map placed below the module that needs it does nothing for that module.

Check
grep -nE 'importmap|type="module"' index.html

Fix

Add a prefix entry with a trailing slash on both sides, and move every importmap script above the first module script.

Fix
<script type="importmap">
  {
    "imports": {
      "my-lib": "/vendor/my-lib/index.js",
      "my-lib/": "/vendor/my-lib/"
    }
  }
</script>
<script type="module">
  import { helper } from "my-lib/extras/helper.js";
</script>

Verify

The subpath import resolves to /vendor/my-lib/extras/helper.js in the Network tab.

Verify
curl -sI http://localhost:8080/vendor/my-lib/extras/helper.js | head -n 1

Confirmed by developer.mozilla.org

Did this fix it?

Cause 4 of 4

The bare import runs inside a module worker

You seeThe page's own imports work, but the failing stack points at a file started with new Worker(url, { type: "module" })

Check

Import maps only apply to modules loaded into the document. A module worker or worklet ignores the page's map, so a package name that resolves on the page fails inside the worker.

Check
grep -rn "type: \"module\"" --include=*.js .

Fix

Inside the worker, import by relative path or full URL, or bundle the worker file so its imports are resolved at build time.

Fix
// worker.js
import { parse } from "/vendor/my-lib/index.js";   // not "my-lib"

Verify

The worker starts and posts its first message without a TypeError in the worker's console.

Verify
new Worker('/worker.js', { type: 'module' })

Confirmed by developer.mozilla.org

Did this fix it?

How do I stop it coming back?

  • Put the importmap script first in the head, ahead of every module script.
  • For every package with deep imports, add both the name and the name-with-slash keys at the same time.
  • Run a grep for imports ending in .js without a ./, ../ or / prefix as a lint step in CI.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.