요약

  • nodejs가 ES Module을 CommonJS 모듈 로더로 불러서 생긴 문제로 next.config에서 해당되는 패키지를 transpilePackages에 적용한다.

내용

  • vote_web 프로젝트를 빌드하고 실행하면 아래와 같은 오류 발생
Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'axios' imported from C:\NFT_dev\xpla-vote\vote_web\.next\server\pages\_document.js
Did you mean to import axios-npm-1.5.1-6bc68e7d25-4444f06601.zip/node_modules/axios/dist/node/axios.cjs?
    at new NodeError (node:internal/errors:387:5)
    at packageResolve (node:internal/modules/esm/resolve:852:9)
    at moduleResolve (node:internal/modules/esm/resolve:901:20)
    at defaultResolve (node:internal/modules/esm/resolve:1115:11)
    at nextResolve (node:internal/modules/esm/loader:163:28)
    at ESMLoader.resolve (node:internal/modules/esm/loader:841:30)
    at ESMLoader.getModuleJob (node:internal/modules/esm/loader:424:18)
    at ESMLoader.import (node:internal/modules/esm/loader:525:22)
    at importModuleDynamically (node:internal/modules/cjs/loader:1129:29)
    at importModuleDynamicallyWrapper (node:internal/vm/module:438:21)
    at importModuleDynamically (node:vm:389:46)
    at importModuleDynamicallyCallback (node:internal/process/esm_loader:35:14)
    at Object.9648 (C:\NFT_dev\xpla-vote\vote_web\.next\server\pages\_document.js:162:1)
    at __webpack_require__ (C:\NFT_dev\xpla-vote\vote_web\.next\server\webpack-runtime.js:25:42)
    at C:\NFT_dev\xpla-vote\vote_web\.next\server\chunks\6184.js:17:63
    at Function.__webpack_require__.a (C:\NFT_dev\xpla-vote\vote_web\.next\server\webpack-runtime.js:89:13) {
  code: 'ERR_MODULE_NOT_FOUND'
  • 해당 코드에서 오류가 난 부분을 보면 아래와 같음
...
/***/ 997:
/***/ ((module) => {
 
module.exports = require("react/jsx-runtime");
 
/***/ }),
 
/***/ 9648:
/***/ ((module) => {
 
module.exports = import("axios");;
 
/***/ })
...
/// next.config.js
const nextConfig = {
    ...
    transpilePackages: ['axios', 'redux-saga'],
}

참고