NextJS 빌드 에러
NextJS에 Mysql를 사용하여, API를 통하지 않고 DB에서 직접 데이터를 가져오는 방식으로 서버 컴포넌트를 만들고 있었다.
'npm run dev'에서는 문제가 없이 잘 동작하였다.
하지만, 'npm run build'를 하니 'Module not found: Can't resolve 'net', Module not found: Can't resolve tls' 에러가 나타났다.
아래는 에러 내용이다.
> next build
▲ Next.js 15.2.4
- Environments: .env.local
Creating an optimized production build ...
Failed to compile.
./node_modules/mysql2/lib/auth_plugins/sha256_password.js
Module not found: Can't resolve 'tls'
https://nextjs.org/docs/messages/module-not-found
Import trace for requested module:
./node_modules/mysql2/lib/commands/auth_switch.js
./node_modules/mysql2/lib/commands/client_handshake.js
./node_modules/mysql2/lib/commands/index.js
./node_modules/mysql2/lib/base/connection.js
./node_modules/mysql2/lib/promise/connection.js
./node_modules/mysql2/promise.js
./src/db/index.ts
./src/db/utils.ts
./src/components/ai-model-list/index.tsx
./src/pages/ai-models.tsx
./node_modules/mysql2/lib/base/connection.js
Module not found: Can't resolve 'net'
https://nextjs.org/docs/messages/module-not-found
Import trace for requested module:
./node_modules/mysql2/lib/promise/connection.js
./node_modules/mysql2/promise.js
./src/db/index.ts
./src/db/utils.ts
./src/components/ai-model-list/index.tsx
./src/pages/ai-models.tsx
./node_modules/mysql2/lib/base/connection.js
Module not found: Can't resolve 'tls'
https://nextjs.org/docs/messages/module-not-found
Import trace for requested module:
./node_modules/mysql2/lib/promise/connection.js
./node_modules/mysql2/promise.js
./src/db/index.ts
./src/db/utils.ts
./src/components/ai-model-list/index.tsx
./src/pages/ai-models.tsx
> Build failed because of webpack errors
해결 방법
npm run install net
npm run install tls
위의 두개의 npm을 직접 설치하여 해결하였다.
net은 nodejs를 설치하면 일반적으로 포함되어 있지만, NextJS에서는 그렇지 않은 것이다.
npm install mysql2 명령어 실행 시, 상기의 패키지들이 자동으로 설치되지 않아 직접 입력해야 했던 것이다.
2025년 10월 29일
GPT, Gemini, Cluade 3개 AI가 모두 해결하지 못하고 코드를 변경해야한다는 말을 하고 있다.
'NextJS' 카테고리의 다른 글
| [NextAuth] 비밀번호 변경 및 프론트엔드에서 비밀번호 변경 요청 보내기 - 10 (0) | 2025.11.20 |
|---|---|
| [NextJS] SSR과 Hydration 에러 해결 방법 (0) | 2025.10.21 |
| NextJS 로딩 상태 처리 (0) | 2025.10.20 |
| [NextJS] 서버 컴포넌트에서 DB 직접 접근 (0) | 2025.10.19 |
| [NextJS] 서버 컴포넌트에서 데이터 직접 가져오기 (Server-Side Fetching) (0) | 2025.10.18 |