Nuxt 3 deployment fails at start with 'Cannot find module' for an @/ alias — which log phase to trust
0 reputation · 16 Oct 2021, 01:35 UTC
0 reputation · 16 Oct 2021, 01:35 UTC
A Nuxt 3 (v3.4+) app builds cleanly with nuxt build, but the deployed server exits during startup with an error resembling Cannot find module '@/utils/config'. The build log shows no warning, so the failure only surfaces when Nitro actually runs the server bundle.
The suspicion is that @/ aliases are resolved for client-side code by Vite, while server-side files bundled by Nitro only resolve them if the alias is declared in nuxt.config.ts or tsconfig.json — and that this behavior depends on whether a src directory exists. The deployment target is a Node server preset, and the output directory is the default .output.
The uncertainty is diagnostic rather than configurational: since the build phase reports success, it is unclear which stage of the log (build output, Nitro prerender, or the node .output/server/index.mjs runtime) is the authoritative place to confirm a missing server-side alias before redeploying.
Questions:
src directory change Nitro's automatic alias resolution, and where is this behavior specified?.output/server/index.mjs — to confirm the log signature matches the deployment failure?35775 reputation · 16 Oct 2021, 06:49 UTC
The authoritative place to confirm the missing @/ alias is the runtime log when you start the Nitro server (node .output/server/index.mjs). The build phase shows no warning because Vite resolves the alias for client code, but Nitro does not automatically apply the same alias for server‑side modules unless it is declared explicitly.
nuxt build Vite processes client bundles and resolves @/ using the default mapping (./src when a src folder exists).nuxt.config.ts under build.aliases or via tsconfig.json compilerOptions.paths.node .output/server/index.mjs reproduces the exact Cannot find module '@/utils/config' error, making the runtime log the definitive diagnostic source.nuxt build (should exit with code 0).node .output/server/index.mjs.nuxt.config.ts:export default defineNuxtConfig({
build: {
aliases: {
'@/': './src'
}
}
})
Alternatively, add a paths entry in tsconfig.json:
{
"compilerOptions": {
"paths": {
"@/*": ["src/*"]
}
}
}
After adding the alias, repeat nuxt build and node .output/server/index.mjs; the server should start without the module error.
If you are using a custom src layout or a monorepo, confirm the exact path you want @/ to point to; otherwise the above steps are sufficient.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.