Vercel build cannot resolve src/* path aliases in NestJS project

Hi Vercel team

I’m deploying a NestJS project to Vercel and hitting an issue with module resolution.
On local development and other hosting providers, my imports like this work fine:

import Roles from 'src/common/guards/verify-jwt.guard'

When deployed to Vercel, the build succeeds but runtime fails with:

this is i define databaseModule :

import { DatabaseModule } from 'src/modules/database/database.module';

i think the error because vercel cannot understand the “src” path, i founded the solution by remove the src/ and write actual manual path like “../../…” instead of “src/” but it have more than 50 constants and props imported automaticly, so what is your suggestion to fix that?

1 Like

Seeing this same issue, with a slightly different error logged:

Cannot find module '@api/auth/auth.module'
Require stack:
- /var/task/apps/api/src/app.module.js
- /var/task/apps/api/src/main.js
Did you forget to add it to "dependencies" in `package.json`?
Node.js process exited with exit status: 1. The logs above can help with debugging the issue.

Looks to me like it is finding the proper .js files but somewhere in the import chain it starts looking at the uncompiled aliased paths instead of the built relative ones.

I also don’t see it as a feasible solution to skip using import aliases in a large codebase, especially a monorepo.

Hi @morezadev @fullyherge, I’m sorry you are facing this issue. Can you share a public repo or a minimal reproducible example. That will let us all work together from the same code to figure out what’s going wrong.

What does your tsconfig look like?

Absolutely, here’s an attempt at a minimal repro:

Here’s the Vercel project, if you can check the logs:

1 Like

Hi @fullyherge, thanks. Let me try to recreate this.

Also seeing this present in the same way

Hi @morezadev, welcome to the Vercel Community!

I found the following solution from a StackOverflow post:

The solution (for now, at least) is to replace all your imports with relative paths, like so :

from → import { UsersService } from 'src/users/users.service';

to ----> import { UsersService } from '../users/users.service';

Have you tried this?

Would you really expect someone to rewrite all the imports from the src alias (it is a standard in Nest.js) to infinite “../../../../”?

Hi @morezadev, just checking in on your issue with the Vercel build and path aliases in your NestJS project. Did you manage to find a solution, or do you still need assistance? Happy to help.