Maaz Zindani

Loading the index…

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

vite build stops with Rollup failed to resolve import

Error[vite]: Rollup failed to resolve import "x" from "src/main.js".

Short answer

The build could not find the module named in the message. Read which import and which file it names. Usually the package is not installed where the build runs, an alias is relative, or the module is meant to be loaded from a CDN and must be declared external.

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

Fix it fast

  1. 1Find out which cause you have

    Take the module name from the message and ask npm whether this project has it installed.

    npm ls <module-name>
  2. 2Match what you see, then jump to its fix

    1. 1npm ls prints (empty) for the module, usually only on CI or a serverThe package is not installed where the build runsFix
    2. 2The unresolved specifier starts with your alias, such as @/components/ButtonA relative alias value was not turned into a pathFix
    3. 3The module is a CDN global or a runtime-provided package, and the message ends with the externalize hintThe module is loaded from outside the bundle on purposeFix

What does the error look like?

  • [vite]: Rollup failed to resolve import "x" from "src/main.js".
  • This is most likely unintended because it can break your application at runtime.
  • If you do want to externalize this module explicitly add it to `build.rollupOptions.external`

Cause 1 of 3

The package is not installed where the build runs

1Check

Compare package.json with what the build machine installed. A package kept only in devDependencies is skipped when npm installs with NODE_ENV=production or --omit=dev.

grep -n "<module-name>" package.json
echo $NODE_ENV

2Fix

Install the package, and put it in dependencies or make sure the build step installs devDependencies too (--production=false).

npm install <module-name>
# or, in a production-mode environment
npm install --production=false

3Verify

The module is listed, then the build passes.

npm ls <module-name>
npm run build

Confirmed by docs.npmjs.com

Did this fix it?

Cause 2 of 3

A relative alias value was not turned into a path

1Check

Vite uses a relative alias value as written and does not resolve it to a file system path.

grep -n "alias" -A6 vite.config.*

2Fix

Alias to an absolute path.

// vite.config.js
import { fileURLToPath, URL } from 'node:url'
export default defineConfig({
  resolve: {
    alias: { '@': fileURLToPath(new URL('./src', import.meta.url)) },
  },
})

3Verify

Run the build again.

npm run build

Confirmed by vite.dev

Did this fix it?

Cause 3 of 3

The module is loaded from outside the bundle on purpose

1Check

The error text itself says to add the module to the external option if you want to leave it out of the bundle.

grep -n "external" vite.config.*

2Fix

Declare it as external. Vite 7 reads it from build.rollupOptions; Vite 8 renames the option to build.rolldownOptions. The module then stays as an import statement in the output.

// vite.config.js (Vite 7)
export default defineConfig({
  build: { rollupOptions: { external: ['some-cdn-module'] } },
})

3Verify

Build and confirm the import is left in the output.

npm run build
grep -rn "some-cdn-module" dist/assets | head -3

Confirmed by rolldown.rs

Did this fix it?

How do I stop it coming back?

  • Run npm ci in CI so the build installs what the lockfile lists, and keep build tools in dependencies if the host omits dev packages.
  • Use absolute paths in resolve.alias.
  • Only mark a module external when something at runtime really provides it.

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.