Maaz Zindani

Loading the index…

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

Vite warns about a script tag in index.html it cannot bundle

Error<script src="..."> in "/index.html" can't be bundled without type="module" attribute

Short 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.

  • Applies to Vite 7, Node.js 22
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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.html
  2. 2Match what you see, then jump to its fix

    1. 1The warning names a script such as ./config.js or ./vendor/lib.jsA classic script with a local path sits outside public/Fix
    2. 2The script is loaded from a CDN or a file you manage yourselfYou want Vite to leave the tag alone on purposeFix
    3. 3The script uses import or export and works with type=module in the browserThe file is really a module and should be bundledFix

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.html

2Fix

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.js

Confirmed by vite.dev

Did this fix it?

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.html

2Fix

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.html

Confirmed by vite.dev

Did this fix it?

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.js

2Fix

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/assets

Confirmed by vite.dev

Did this fix it?

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 fix

Next step

Want this handled for good?

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