TypeError: Failed to resolve module specifier (JavaScript) — how to fix
Uncaught TypeError: Failed to resolve module specifier "lodash". Relative references must start with either "/", "./", or "../".
Quick fix: Use a bundler/dev server that resolves npm imports (npm create vite@latest, then npm run dev), or add an import map: <script type="importmap">{"imports":{"lodash":"https://esm.sh/lodash"}}</script>. For your own files, use a relative path with the extension: import x from "./utils.js".
What it means
In Node.js and bundlers, import _ from "lodash" is looked up in node_modules. A browser does no such lookup. For native ES modules it only accepts:
- full URLs (
https://...) - paths starting with
/,./or../ - names defined in an import map
Anything else is a bare specifier and fails with this error.
Cause 1: serving source files without a bundler
You installed a package with npm and open index.html (or serve it with a plain static server):
<script type="module" src="main.js"></script>
// main.js
import confetti from "canvas-confetti"; // bare specifier - fails
Fix: use a dev server/bundler that rewrites these imports. Vite is the simplest:
npm create vite@latest my-app
cd my-app
npm install
npm install canvas-confetti
npm run dev
For production, npm run build produces bundled files in dist/ with no bare imports left. Webpack, Parcel, esbuild and Rollup do the same job.
Cause 2: no build step wanted - use an import map
Map names to URLs in the HTML, before any module script:
<script type="importmap">
{
"imports": {
"lodash": "https://esm.sh/lodash@4.17.21",
"vue": "https://unpkg.com/vue@3/dist/vue.esm-browser.js"
}
}
</script>
<script type="module" src="main.js"></script>
Now import _ from "lodash" works. Import maps are supported in all current major browsers. The URL must point to an ES module build, not a CommonJS file.
You can also import straight from a URL without a map:
import confetti from "https://esm.sh/canvas-confetti";
Cause 3: your own file imported without ./
import { sum } from "utils.js"; // treated as a bare specifier
Fix:
import { sum } from "./utils.js";
Browsers also need the file extension; ./utils without .js will 404 unless your server adds it.
Cause 4: output of TypeScript (tsc) loaded directly
tsc does not rewrite package imports, so compiled .js files still contain import ... from "react". Either bundle the output, or add an import map for each package.
Cause 5: Electron / extension contexts
In Electron renderer processes and browser extensions the same browser rules apply. Bundle the renderer code, or load modules by relative path.
Last updated 2026-12-01.