로컬 벡터 DB가 안 가져와지네
개요
- 해커톤 당시 아직 기술부채가 있는 app router를 과감히 버리고page router로 진행
- 생성형 AI를 사용하여 RAG를 통해 원하는 키워드를 뽑아내는 로직 구현
- 당시 pages/api라우터였고HNSWLib라는 Node 전용 벡터스토어 패키지로 벡터화를 진행하였음
문제
- 해커톤 프로젝트를 실사용으로 올리기 위해 과감히 app router로 마이그레이션 작업
- app router에서의- api route사용법은 좀 다른 것을 인지하게 되었음
- 로직은 그대로 가져왔으나 500에러 발생, 에러 메세지는 아래와 같았음.
⨯ Error: Could not import hnswlib-node. Please install hnswlib-node as a dependency with, e.g. `npm install -S hnswlib-node`.
Error: Cannot read properties of undefined (reading 'indexOf')
    at HNSWLib.imports (webpack-internal:///(rsc)/./node_modules/@langchain/community/dist/vectorstores/hnswlib.js:275:19)
    at async HNSWLib.getHierarchicalNSW (webpack-internal:///(rsc)/./node_modules/@langchain/community/dist/vectorstores/hnswlib.js:58:37)
    at async HNSWLib.load (webpack-internal:///(rsc)/./node_modules/@langchain/community/dist/vectorstores/hnswlib.js:217:23)
    ...
- node_modules에 해당 패키지 정상적으로 설치 되었음을 확인
- 버전이슈확인 하였으나 아님을 확인
- pages/api에서는 정상적으로 동작함을 확인
결론
- app router의- api router에서 문제가 있는 것을 확신
- 깃헙 이슈에 해당 패키지 검색해서 아래 관련 이슈 확인
- https://github.com/langchain-ai/langchainjs/issues/943
- Nextjs 13버전의 route handler로 전환하자 문제가 발생하는 하는 것을 확인
- 공식적으로 아직 이유에 대해서 밝혀진 바가 없지만 아래 next.config설정으로 해결
/** @type {import('next').NextConfig} */
const nextConfig = {
  reactStrictMode: true,
  webpack: config => {
    config.externals = [...config.externals, "hnswlib-node"];
    return config;
  },
};
export default nextConfig;