How to Fix “Failed to resolve module specifier” in JavaScript
When the browser console shows Failed to resolve module specifier "react", it means the browser does not know which URL to load for the "react" in import ... from "react". Unlike Node.js, the browser does not search node_modules for packages. Either open the page through a build tool such as Vite, or map "react" to a URL with an import map, and the error goes away.
Install React with npm, write <script type="module"> in an HTML file, and open it from a server without any build tool. If nothing appears on the screen, this error is occurring. I deliberately reproduced the error in my test environment and copied the message as it appeared. I also confirmed that the error disappears with each of the two fixes.
The test environment was Ubuntu 24.04, Node.js 22.23.5, and Chromium 158.0.8127.36 (headless) launched with Playwright 1.63.0. I used React 19.3.1, Vite 8.3.8, and esbuild 0.28.2. The page was served from a test server written in Node.js at http://localhost:8080. I took the message from the same exception information that the DevTools console receives, through Playwright. I did not test the messages in Firefox or Safari, and I did not test putting a CDN URL in an import map.
The Full Error
I loaded the following main.js from broken.html with <script type="module">. The script only displays the React version on the page.
import { version } from "react";
document.getElementById("out").textContent = `React ${version} を読み込みました`;
<!doctype html>
<html lang="ja">
<head>
<meta charset="utf-8">
<title>broken</title>
</head>
<body>
<p id="out">(スクリプトは動いていません)</p>
<script type="module" src="./main.js"></script>
</body>
</html>
React was installed in node_modules with npm install react. When I opened this page, the console showed the following error.
Uncaught TypeError: Failed to resolve module specifier "react". Relative references must start with either "/", "./", or "../".
The message means "the module specifier "react" cannot be resolved; relative references must start with /, ./, or ../." None of the lines in main.js ran, and the page still showed its placeholder text, "(The script has not run)".
The Browser Does Not Look in node_modules
How a string after from (the module specifier) is handled depends on the environment that runs it. MDN's explanation of module specifier resolution in the import reference divides specifiers into three kinds:
- Relative specifiers that start with
/,./, or../ - Specifiers that can be read as URLs, such as
https://example.com/x.js - Anything else, a "bare specifier", such as
"react", which is only a package name
When Node.js receives a bare specifier, it searches node_modules for that package. The browser does not. The same explanation says that a bare specifier must be resolved with an import map in the browser. MDN's guide to modules on the web also says that a bare specifier with no mapping in an import map throws a TypeError. That is the error I got.
Following the wording of the error and rewriting the import to a path such as "/node_modules/react/index.js" does not fix it. It only changes the error to a different one. I confirmed this in the import map section below.
Fix 1: Open the Page Through Vite
If you build the UI with React, you can use a build tool such as Vite. The build tool rewrites the bare specifier into a URL that the browser can load.
I created a src/main.js that also renders with createRoot, and loaded it from index.html with <script type="module" src="/src/main.js">. The import statements stayed as from "react".
import { version, createElement } from "react";
import { createRoot } from "react-dom/client";
document.getElementById("out").textContent = `React ${version} を読み込みました`;
createRoot(document.getElementById("root")).render(createElement("p", null, "React で描画しました"));
I ran npx vite in the vite-app folder containing these two files to start the development server, then opened http://localhost:5173/. There was no error, and both "React 19.3.1 loaded" and "Rendered with React" were displayed.
When I fetched src/main.js from the development server with curl, the import lines had been rewritten as follows. (I have replaced the working folder of the test environment with /path/to/app.)
import __vite__cjsImport1_reactDom_client from "/@fs/path/to/app/node_modules/.vite/deps/react-dom_client.js?v=1afd1bd2";
"react-dom/client" has become the URL of a file that Vite prepared. According to the dependency pre-bundling section of the Vite documentation, Vite converts dependency packages distributed as CommonJS into ES modules beforehand.
To publish, run npx vite build. The 12 modules were combined into one JavaScript file in dist/assets/ (219.27 kB), and no from "react" string remained in it. Opening it with npx vite preview also showed both lines without any error.
Fix 2: Map "react" to a URL With an Import Map
If you want the page to work with only HTML and no build tool, use an import map. Inside <script type="importmap">, write a JSON imports object that pairs each specifier with the URL to load in its place.
Pointing to react/index.js in node_modules Does Not Work
First, I mapped "react" to a file inside node_modules.
<script type="importmap">
{ "imports": { "react": "/node_modules/react/index.js" } }
</script>
The server returned /node_modules/react/index.js, but this time the following error appeared, and the screen still did not change.
Uncaught SyntaxError: The requested module 'react' does not provide an export named 'version'
I opened index.js in the npm react package. Its contents were as follows.
'use strict';
if (process.env.NODE_ENV === 'production') {
module.exports = require('./cjs/react.production.js');
} else {
module.exports = require('./cjs/react.development.js');
}
This file uses the CommonJS style, with module.exports and require, and has no export statements at all. The browser reads this file as an ES module, so it reports that no export named version exists.
I also tried writing import { version } from "/node_modules/react/index.js"; directly in main.js, without an import map. It loads the same file and produced the following error.
Uncaught SyntaxError: The requested module '/node_modules/react/index.js' does not provide an export named 'version'
Provide an ES Module File and Point to It
The value in an import map must be the URL of a file written as an ES module. For this test, I used esbuild to create one ES module file from npm's react package. The entry file lists the names I need to export.
export { version, createElement, useState, StrictMode } from "react";
npx esbuild vendor-entry.js --bundle --format=esm --define:process.env.NODE_ENV='"production"' --outfile=public/vendor/react.js
The resulting vendor/react.js contained an export { … } statement that exports StrictMode, createElement, useState, and version. The test server serves the contents of public/ as the site root (and also serves /node_modules/ for comparison), so the URL of this file is /vendor/react.js. I pointed the import map at this file.
<!doctype html>
<html lang="ja">
<head>
<meta charset="utf-8">
<title>importmap</title>
<script type="importmap">
{ "imports": { "react": "/vendor/react.js" } }
</script>
</head>
<body>
<p id="out">(スクリプトは動いていません)</p>
<script type="module" src="./main.js"></script>
</body>
</html>
main.js stayed unchanged, still using from "react". When I opened this page, the console showed no error, and "React 19.3.1 loaded" was displayed. The server log showed /vendor/react.js being loaded after main.js.
Place the import map before any <script> that uses it. MDN's import map explanation also says that the map must be placed before every <script> that uses its specifiers, so that it is processed first.
As in the MDN example, a value can also be a URL starting with https://. In that case too, the target must be a file that can be read as an ES module.
Which Fix to Use
If you build the UI with React, the Vite approach is the better choice. As shown above, packages used together with React, such as react-dom/client, are converted and bundled by Vite. With an import map, you need to prepare an ES module file for each package you map. The import map is the way to run a small page without a build step. In that case, first check that the file you point to is an ES module and not a CommonJS file.





