vercel에서 prisma 사용한 프로젝트 에러

kimyz·2024년 1월 25일

로컬에서는 마냥 잘 돌아가던 프로젝트를 vercel에 배포했는데 갑자기 에러가 났다.

Uncaught (in promise) SyntaxError: Unexpected token '<', "<!DOCTYPE "... is not valid JSON

에러 내용은 그냥 이런 내용이었다.
근데 그냥 저 내용이랑은 상관 없이 프리즈마 & 버셀의 문제였다.

https://www.prisma.io/docs/orm/more/help-and-troubleshooting/help-articles/vercel-caching-issue

This occurs because Vercel caches the dependencies of your project 
until one of those dependencies changes. 
It does this to allow faster builds, and while this is typically 
a good thing, it causes some problems for Prisma Client.

Prisma uses a postinstall hook to generate Prisma Client 
when dependencies are installed. Because Vercel uses cached modules, 
this postinstall hook never gets run in subsequent deployments 
after the initial deployment. 
This results in Prisma Client becoming out of sync 
with your database schema.

위 주소의 윗부분을 참고하면, vercel은 프로젝트 의존성을 캐시해서 사용을 하기 때문에 빠르긴 하지만 프리즈마 클라이언트에서는 문제를 만들 수 있다.
프리즈마는 postinstall 이라는 훅을 통해서 클라이언트를 생성하는데,
vercel이 캐싱된 모듈을 사용하니까 저 훅이 처음 배포 때 빼고는 돌아가지를 않는다.

그래서 에러가 났던 것,,

해결방법

배포가 될 때마다 클라이언트를 생성하도록 명시해주면 됨!

배포하기 전에 prisma generate를 항상 실행할 수도 있고

1. package.json 파일 수정

"scripts" {
	"postinstall": "prisma generate"
    // 아니면
    "bhild": "prisma generate && next build(원래 빌드 명령어)"
}

2. vercel 설정

vercel 프로젝트 대시보드 > Settings > General
Build & Development Settings 들어가서 Build 명령어를 변경해 주면 됨

옆에 override를 활성화시키고 내용을 아래로 바꿔준다.

npx prisma generate && next build

추가

이거 하고 다른 에러가 날 수도 있는데 뭔가 env와 관련된 에러가 있다면
재배포를 하면 해결이 될 수도 있따 .. !

profile
😛

0개의 댓글