문제: [Next.js] Module not found: Can't resolve 'fs'
이유: 일반적으로 클라이언트 측에서 nodejs 라이브러리를 사용하려고 할 때 발생하는 오류, 클라이언트(브라우저)에서는 사용이 불가능한 패키지 이므로 오류가 발생하게 된 것
해결 방안:
수정 전
/** @type {import('next').NextConfig} */
const nextConfig = {
reactStrictMode: true,
};
module.exports = nextConfig;
webpack4의 경우
/** @type {import('next').NextConfig} */
const nextConfig = {
reactStrictMode: true,
webpack: (config, { isServer }) => {
// Fixes npm packages that depend on `fs` module
if (!isServer) {
config.node = {
fs: 'empty'
}
}
return config
}
};
module.exports = nextConfig;
webpack5의 경우
/** @type {import('next').NextConfig} */
const nextConfig = {
reactStrictMode: true,
webpack5: true,
webpack: (config) => {
config.resolve.fallback = { fs: false };
return config;
},
};
module.exports = nextConfig;