Vite warns about a script tag in index.html it cannot bundle
<script src="..."> in "/index.html" can't be bundled without type="module" attributeShort answer
Vite only bundles scripts marked type=module. A classic script tag with a local src is left as it is, and Vite warns. It is a warning, not a build failure. Keep the file untouched by putting it in public/ and using an absolute path, or add vite-ignore to the tag.
Fix it fast
1Find out which cause you have
List the script tags in index.html that lack type=module, and see whether each file is ESM or a classic global script.
grep -n "<script" index.html2Match what you see, then jump to its fix
- 1
The warning names a script such as ./config.js or ./vendor/lib.jsA classic script with a local path sits outside public/Fix - 2
The script is loaded from a CDN or a file you manage yourselfYou want Vite to leave the tag alone on purposeFix - 3
The script uses import or export and works with type=module in the browserThe file is really a module and should be bundledFix
- 1
What does the error look like?
<script src="..."> in "/index.html" can't be bundled without type="module" attribute
Cause 1 of 3
A classic script with a local path sits outside public/
1Check
Vite handles <script type="module" src> as part of the build. A plain script is a different kind of file, and it is not part of the module graph.
ls public/ && grep -n "<script" index.html2Fix
Put the file in public/ and reference it with a root absolute path. Files there are copied to the root of dist as-is, so the file keeps its name and is not processed.
<!-- index.html: file lives at public/config.js -->
<script src="/config.js"></script>3Verify
Build and confirm the file sits at the root of dist/.
npm run build
ls dist/config.jsConfirmed by vite.dev
Cause 2 of 3
You want Vite to leave the tag alone on purpose
1Check
Vite lets you opt out of HTML processing for a single element with the vite-ignore attribute; the docs mention external assets and CDNs as uses.
grep -n "vite-ignore" index.html2Fix
Add vite-ignore to that tag.
<script vite-ignore src="https://cdn.example.com/lib.js"></script>3Verify
Run the build and check that the tag is untouched in dist/index.html.
npm run build
grep -n "cdn.example.com" dist/index.htmlConfirmed by vite.dev
Cause 3 of 3
The file is really a module and should be bundled
1Check
A script written as an ES module should say so. Vite bundles it only when it has the attribute.
grep -n "^import\|^export" src/legacy.js2Fix
Add type=module so Vite includes it in the build.
<script type="module" src="/src/legacy.js"></script>3Verify
Build and look for the hashed file in dist/assets.
npm run build
ls dist/assetsConfirmed by vite.dev
How do I stop it coming back?
- Decide per script: module in src/, or untouched file in public/.
- Never add type=module to a classic global script; it is not an ES module.
- Prefer importing libraries from npm in your entry file instead of loading them with script tags.
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.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Web apps
tsconfig rejects allowImportingTsExtensions
An error that allowImportingTsExtensions needs noEmit or emitDeclarationOnly
allowImportingTsExtensions lets files import each other with .ts or .tsx extensions, and TypeScript only allows it together with noEmit or emitDeclarationOnly.
2 causes, each with a fixWeb apps
Apollo Server rejects a request as a potential CSRF
HTTP 400 Bad Request from the GraphQL endpoint
Apollo Server's CSRF prevention is on by default.
3 causes, each with a fixWeb apps
React shows Attempted import error when importing a component or function
Attempted import error:
The build found the file but not the name you asked for.
2 causes, each with a fixWeb apps
AxiosError Network Error with code ERR_NETWORK in a React app
Network Error
ERR_NETWORK means the request produced no readable response.
4 causes, each with a fixWeb apps
Axios get works with a URL string but ignores the object passed as the second argument
The server log shows a GET with no query string
axios.get takes a URL and an optional config object.
3 causes, each with a fixWeb apps
Can't resolve '../axios' in a React project
Module not found: Error: Can't resolve '../axios'
A path that starts with ./ or ../ is a file path, not the axios package.
3 causes, each with a fix