[에러] next.js에서 fs

옥수수의 코딩이야기·2023년 2월 5일
0

에러

목록 보기
6/18
post-thumbnail

문제: [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;

참고
https://cocoon1787.tistory.com/851

profile
프론트엔드 공부중 기억은 블로그가 대신합니다.

0개의 댓글