vite build stops with Rollup failed to resolve import
[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.
Fix it fast
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>2Match what you see, then jump to its fix
- 1
npm ls prints (empty) for the module, usually only on CI or a serverThe package is not installed where the build runsFix - 2
The unresolved specifier starts with your alias, such as @/components/ButtonA relative alias value was not turned into a pathFix - 3
The 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
- 1
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_ENV2Fix
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=false3Verify
The module is listed, then the build passes.
npm ls <module-name>
npm run buildConfirmed by docs.npmjs.com
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 buildConfirmed by vite.dev
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 -3Confirmed by rolldown.rs
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 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