Fix Chrome Extension HTML Import Errors
In this article
To fix HTML import errors in a Chrome extension, you must replace deprecated HTML Imports with standard JavaScript fetch() or import statements, because Chrome removed HTML Imports support in version 80 (released February 2020). If you are seeing errors like "Failed to load resource: net::ERR_FILE_NOT_FOUND" or "Unexpected token '<'", you are likely trying to use an <link rel="import"> tag, which no longer works. The solution is to load your HTML fragments via fetch() and inject them into the DOM, or use a bundler like Webpack to inline everything into a single file.
Why HTML Imports Stopped Working
HTML Imports were a proposed web standard that let you include an HTML file inside another using <link rel="import" href="file.html">. They were part of the Web Components v0 specification, but the standards community abandoned them in favor of ES modules and the <template> tag. Chrome shipped HTML Imports in version 36 (2014) but removed them in version 80. Firefox and Safari never implemented them. As a result, any Chrome extension code written before 2020 that relies on HTML Imports will break in current Chrome versions.
Common Import Error Messages and Their Causes
- "Failed to load resource: net::ERR_FILE_NOT_FOUND" – The path in your
hreforsrcattribute is wrong, or the file is not listed inweb_accessible_resourcesin your manifest.json. - "Unexpected token '<'" – You tried to use
importorrequire()on an HTML file, but the JavaScript engine expects a JS module. This happens when you writeimport './template.html'without a bundler. - "Refused to load the script because it violates the following Content Security Policy directive" – Your extension's CSP blocks
eval()or inline scripts. If you are using a bundler that injects inline scripts, you must adjust the CSP in manifest.json. - "Cannot use import statement outside a module" – You are using ES module syntax in a non-module script. In extension popups or background scripts, you need to set
"type": "module"in the script tag or use a bundler.
How to Fix: Use fetch() Instead of HTML Imports
The simplest replacement is to fetch the HTML file at runtime and insert it into the DOM. Here is a step-by-step approach:
- Make sure the HTML file is listed in
web_accessible_resourcesin your manifest.json, so the extension can access it. - In your JavaScript, call
fetch(chrome.runtime.getURL('templates/panel.html')). - Convert the response to text with
.then(r => r.text()). - Insert the text into a container element using
innerHTMLorDOMParser.
Example code:
async function loadTemplate() {
const response = await fetch(chrome.runtime.getURL('templates/panel.html'));
const html = await response.text();
document.getElementById('container').innerHTML = html;
}
loadTemplate();This works in popup pages, options pages, and content scripts (though content scripts have restrictions on chrome.runtime.getURL – you need to pass the URL via messaging if you are in an isolated world).
Alternative: Use a Bundler (Webpack, Vite, or Rollup)
If your extension has many HTML fragments or you want to avoid runtime fetching, use a bundler. Webpack with html-loader lets you import HTML directly: import template from './template.html'. The loader converts the HTML into a string at build time, so you get the same result without runtime network requests. This is more reliable because it avoids web_accessible_resources issues and reduces load time. For a minimal setup, you can use Vite's ?raw suffix to import any file as a string: import template from './template.html?raw'. Then you can inject that string into the DOM.
Fixing Import Errors in Manifest V3
Manifest V3 (the current version) enforces stricter Content Security Policy. It disallows remote code and eval(). If you are using a bundler that generates inline scripts, you must either move the script to an external file or set "content_security_policy": {"extension_pages": "script-src 'self'; object-src 'self'"} in your manifest.json. Also, service workers (background scripts in MV3) cannot access the DOM, so you cannot use fetch() to inject HTML into the background page – you must do that in the popup or content script. If you need to load HTML in a service worker, you can use importScripts() for JavaScript files, but not for HTML; you would need to store the HTML as a string constant or use a bundler.
Debugging Steps for Persistent Errors
- Open the extension's popup or page, right-click and select "Inspect" to open DevTools. Check the Console tab for the exact error message.
- Verify the file path in your code. In a popup, relative paths are relative to the popup HTML file. In a content script, you must use
chrome.runtime.getURL()to get an absolute path. - Check that your manifest.json includes the HTML file under
web_accessible_resourcesif you are fetching it from a content script or an injected page. - If you are using ES modules, ensure your script tag has
type="module"and that you are not mixing CommonJS and ES module syntax. - Clear the extension cache by going to
chrome://extensions, clicking the refresh icon on your extension, and reloading the page.
Frequently Asked Questions
Can I still use <link rel="import"> in Chrome extensions?
No. Chrome removed HTML Imports in version 80, so the tag is ignored and produces no error but also no effect. You must use fetch() or a bundler.
Why does my popup show a blank page after I try to import HTML?
This usually happens because the fetch() call fails silently. Check the Console for errors. A common cause is that the HTML file is not in web_accessible_resources, or you used a relative path that resolves to the wrong location.
Can I use import statements in a content script?
Yes, but only if you set "type": "module" in the script tag or use a bundler. However, content scripts run in an isolated world, so you cannot directly access the page's DOM with innerHTML if you inject a string – you need to use document.createElement and set innerHTML on that element, then append it to the page.
What is the difference between fetch() and import for HTML?
fetch() is a runtime network request that returns a promise. import is a static declaration that is resolved at build time if you use a bundler. For small extensions, fetch() is simpler. For larger projects, a bundler gives you better performance and error checking.
Do I need to update my manifest.json for fetch() to work?
Yes, if you are fetching from a content script or an injected page, you must list the HTML file in web_accessible_resources. If you are fetching from the popup or options page, you do not need to list it, but the file must be included in the extension package.
Why do I get a CSP error when I use eval() or inline scripts?
Manifest V3 disallows eval() and inline scripts by default. If your code uses them, you must refactor to use external scripts and avoid eval(). You can also relax the CSP, but that is not recommended for security.