使用 NextJS 进行 Amplify 构建期间出现“错误:无法解析‘bufferutil’”

Dom*_*pek 5 webpack next.js aws-amplify

我尝试在 Amplify 上托管我的 NextJS 应用程序,但是当 Amplify 尝试构建应用程序时,我收到一条错误消息,指出找不到“bufferutil”或“utf-8-validate”模块。这是由于找不到 bufferutil 模块而失败时的输出:

\n
                                 # Starting phase: preBuild\n                                 # Executing command: npm ci\n2021-09-21T19:44:34.448Z [INFO]: > aws-sdk@2.983.0 postinstall /codebuild/output/src886343224/[my_app_path]/node_modules/aws-sdk\n                                 > node scripts/check-node-version.js\n2021-09-21T19:44:34.599Z [INFO]: > @apollo/protobufjs@1.2.2 postinstall /codebuild/output/src886343224/[my_app_path]/node_modules/@apollo/protobufjs\n                                 > node scripts/postinstall\n2021-09-21T19:44:34.653Z [INFO]: > core-js-pure@3.15.2 postinstall /codebuild/output/src886343224/[my_app_path]/node_modules/core-js-pure\n                                 > node -e "try{require('./postinstall')}catch(e){}"\n2021-09-21T19:44:34.693Z [INFO]: [96mThank you for using core-js ([94m https://github.com/zloirock/core-js [96m) for polyfilling JavaScript standard library![0m\n                                 [96mThe project needs your help! Please consider supporting of core-js on Open Collective or Patreon: [0m\n                                 [96m>[94m https://opencollective.com/core-js [0m\n                                 [96m>[94m https://www.patreon.com/zloirock [0m\n                                 [96mAlso, the author of core-js ([94m https://github.com/zloirock [96m) is looking for a good job -)[0m\n2021-09-21T19:44:34.876Z [INFO]: added 1308 packages in 12.354s\n2021-09-21T19:44:34.887Z [INFO]: # Executing command: mkdir out\n2021-09-21T19:44:34.888Z [INFO]: # Completed phase: preBuild\n2021-09-21T19:44:34.889Z [INFO]: # Starting phase: build\n                                 # Executing command: npm run build\n2021-09-21T19:44:35.060Z [INFO]: > slack-app@0.1.0 build /codebuild/output/src886343224/[my_app_path]\n                                 > next build\n2021-09-21T19:44:35.694Z [INFO]: info  - Using webpack 5. Reason: Enabled by default https://nextjs.org/docs/messages/webpack5\n2021-09-21T19:44:35.806Z [INFO]: warn  - No build cache found. Please configure build caching for faster rebuilds. Read more: https://nextjs.org/docs/messages/no-cache\n2021-09-21T19:44:35.830Z [INFO]: Attention: Next.js now collects completely anonymous telemetry regarding usage.\n2021-09-21T19:44:35.830Z [INFO]: This information is used to shape Next.js' roadmap and prioritize features.\n                                 You can learn more, including how to opt-out if you'd not like to participate in this anonymous program, by visiting the following URL:\n                                 https://nextjs.org/telemetry\n2021-09-21T19:44:35.864Z [INFO]: info  - Checking validity of types...\n2021-09-21T19:44:37.467Z [INFO]: info  - Creating an optimized production build...\n2021-09-21T19:45:14.794Z [INFO]: info  - Compiled successfully\n2021-09-21T19:45:14.795Z [INFO]: info  - Collecting page data...\n2021-09-21T19:45:16.189Z [INFO]: info  - Generating static pages (0/7)\n2021-09-21T19:45:17.372Z [INFO]: info  - Generating static pages (1/7)\n2021-09-21T19:45:17.502Z [INFO]: info  - Generating static pages (3/7)\n2021-09-21T19:45:17.511Z [INFO]: info  - Generating static pages (5/7)\n2021-09-21T19:45:17.588Z [INFO]: info  - Generating static pages (7/7)\n2021-09-21T19:45:17.589Z [INFO]: info  - Finalizing page optimization...\n2021-09-21T19:45:17.593Z [INFO]: \n2021-09-21T19:45:17.603Z [INFO]: Page                                Size     First Load JS\n                                 \xe2\x94\x8c \xe2\x97\x8f /                               1.46 kB         234 kB\n                                 \xe2\x94\x9c   /_app                           0 B             230 kB\n                                 \xe2\x94\x9c \xe2\x97\x8b /404                            1.36 kB         231 kB\n                                 \xe2\x94\x9c \xce\xbb /api/graphql                    0 B             230 kB\n                                 \xe2\x94\x9c \xce\xbb /api/slack/redirect             0 B             230 kB\n                                 \xe2\x94\x9c \xce\xbb /api/test                       0 B             230 kB\n                                 \xe2\x94\x9c \xce\xbb /api/user/workspaces            0 B             230 kB\n                                 \xe2\x94\x9c \xce\xbb /api/workspace                  0 B             230 kB\n                                 \xe2\x94\x9c \xce\xbb /dashboard                      2.73 kB         240 kB\n                                 \xe2\x94\x9c \xe2\x97\x8f /dashboard/search               47.3 kB         285 kB\n                                 \xe2\x94\x9c \xe2\x97\x8f /dashboard/share_of_voice       12.2 kB         250 kB\n                                 \xe2\x94\x9c   \xe2\x94\x94 css/6b915c1b3977da366ff4.css  1.53 kB\n                                 \xe2\x94\x9c \xe2\x97\x8f /dashboard/word_cloud           48.1 kB         286 kB\n                                 \xe2\x94\x94 \xe2\x97\x8f /sign_out                       1.49 kB         234 kB\n                                 + First Load JS shared by all       230 kB\n                                 \xe2\x94\x9c chunks/framework.2191d1.js      42.4 kB\n                                 \xe2\x94\x9c chunks/main.588261.js           20.3 kB\n                                 \xe2\x94\x9c chunks/pages/_app.ba1cc2.js     166 kB\n                                 \xe2\x94\x9c chunks/webpack.307141.js        898 B\n                                 \xe2\x94\x94 css/11662bea7c6682700e19.css    4.1 kB\n2021-09-21T19:45:17.604Z [INFO]: \xce\xbb  (Server)  server-side renders at runtime (uses getInitialProps or getServerSideProps)\n                                 \xe2\x97\x8b  (Static)  automatically rendered as static HTML (uses no initial props)\n                                 \xe2\x97\x8f  (SSG)     automatically generated as static HTML + JSON (uses getStaticProps)\n                                 (ISR)     incremental static regeneration (uses revalidate in getStaticProps)\n2021-09-21T19:45:17.760Z [INFO]: Starting SSR Build...\n2021-09-21T19:46:57.093Z [ERROR]: Error: Command failed with exit code 1: node_modules/.bin/next build\n                                  Failed to compile.\n                                  ModuleNotFoundError: Module not found: Error: Can't resolve 'bufferutil' in '/codebuild/output/src886343224/[my_app_path]/node_modules/ws/lib'\n                                  > Build error occurred\n                                  Error: > Build failed because of webpack errors\n                                  at /codebuild/output/src886343224/[my_app_path]/node_modules/next/dist/build/index.js:15:924\n                                  at async Span.traceAsyncFn (/codebuild/output/src886343224/[my_app_path]/node_modules/next/dist/telemetry/trace/trace.js:6:584)\n                                  info  - Using webpack 5. Reason: Enabled by default https://nextjs.org/docs/messages/webpack5\n                                  info  - Checking validity of types...\n                                  info  - Creating an optimized production build...\n                                  at makeError (/root/.//node_modules/execa/lib/error.js:60:11)\n                                  at handlePromise (/root/.//node_modules/execa/index.js:118:26)\n                                  at runMicrotasks (<anonymous>)\n                                  at processTicksAndRejections (internal/process/task_queues.js:97:5)\n                                  at async Builder.build (/root/.//node_modules/@sls-next/lambda-at-edge/dist/build.js:348:13) {\n                                  shortMessage: 'Command failed with exit code 1: node_modules/.bin/next build',\n                                  command: 'node_modules/.bin/next build',\n                                  escapedCommand: '"node_modules/.bin/next" build',\n                                  exitCode: 1,\n                                  signal: undefined,\n                                  signalDescription: undefined,\n                                  stdout: 'info  - Using webpack 5. Reason: Enabled by default https://nextjs.org/docs/messages/webpack5\\n' +\n                                  'info  - Checking validity of types...\\n' +\n                                  'info  - Creating an optimized production build...',\n                                  stderr: 'Failed to compile.\\n' +\n                                  '\\n' +\n                                  "ModuleNotFoundError: Module not found: Error: Can't resolve 'bufferutil' in '/codebuild/output/src886343224/[my_app_path]/node_modules/ws/lib'\\n" +\n                                  '\\n' +\n                                  '\\n' +\n                                  '> Build error occurred\\n' +\n                                  'Error: > Build failed because of webpack errors\\n' +\n                                  '    at /codebuild/output/src886343224/[my_app_path]/node_modules/next/dist/build/index.js:15:924\\n' +\n                                  '    at async Span.traceAsyncFn (/codebuild/output/src886343224/[my_app_path]/node_modules/next/dist/telemetry/trace/trace.js:6:584)',\n                                  failed: true,\n                                  timedOut: false,\n                                  isCanceled: false,\n                                  killed: false\n                                  }\n2021-09-21T19:46:57.095Z [ERROR]: [G[?25h[G[J\n                                  77s \xe2\x80\xba dep5aop5dy2y4 \xe2\x80\xba Error: Command failed with exit code 1: node_modules/.bin/next build\n                                  Failed to compile.\n                                  ModuleNotFoundError: Module not found: Error: Can't resolve 'bufferutil' in '/codebuild/output/src886343224/[my_app_path]/node_modules/ws/lib'\n                                  > Build error occurred\n                                  Error: > Build failed because of webpack errors\n                                  at /codebuild/output/src886343224/[my_app_path]/node_modules/next/dist/build/index.js:15:924\n                                  at async Span.traceAsyncFn (/codebuild/output/src886343224/[my_app_path]/node_modules/next/dist/telemetry/trace/trace.js:6:584)\n                                  info  - Using webpack 5. Reason: Enabled by default https://nextjs.org/docs/messages/webpack5\n                                  info  - Checking validity of types...\n                                  info  - Creating an optimized production build...\n                                  [G[?25h\nTerminating logging...\n
Run Code Online (Sandbox Code Playgroud)\n

我读过一些人说我需要在 webpack 配置中添加一些外部内容,我确实这样做了,但仍然遇到相同的错误:

\n
module.exports = {\n  webpack: (config) => {\n    Object.assign(config.externals, {\n      bufferutil: 'bufferutil',\n      'utf-8-validate': 'utf-8-validate',\n    });\n    \n    return config;\n  },\n};\n
Run Code Online (Sandbox Code Playgroud)\n

这是我的 package.json 文件:

\n
{\n  "name": "",\n  "author": "",\n  "version": "0.1.0",\n  "private": true,\n  "scripts": {\n    "dev": "next dev",\n    "dev:local": "node server.js",\n    "build": "next build",\n    "start": "next start",\n    "lint": "next lint"\n  },\n  "dependencies": {\n    "@apollo/client": "^3.4.7",\n    "@aws-amplify/ui-react": "^1.2.11",\n    "@aws-sdk/client-dynamodb": "^3.29.0",\n    "@babel/core": "^7.14.6",\n    "@babel/preset-env": "^7.14.7",\n    "@babel/runtime": "^7.14.6",\n    "@emotion/react": "^11.4.1",\n    "@emotion/styled": "^11.3.0",\n    "@mui/material": "^5.0.0",\n    "apollo-boost": "^0.4.9",\n    "apollo-server-micro": "^2.25.2",\n    "autoprefixer": "^10.3.1",\n    "aws-amplify": "^4.2.5",\n    "aws-sdk": "^2.983.0",\n    "axios": "^0.21.1",\n    "dynamoose": "^2.8.1",\n    "graphql": "^15.5.1",\n    "micro": "^9.3.4",\n    "next": "^11.0.1",\n    "next-connect": "^0.10.1",\n    "postcss": "^8.3.6",\n    "react": "^17.0.2",\n    "react-content-loader": "^6.0.3",\n    "react-dom": "17.0.2",\n    "react-minimal-pie-chart": "^8.2.0",\n    "react-multi-carousel": "^2.6.5",\n    "react-wordcloud": "^1.2.7",\n    "sentiment": "^5.0.2",\n    "stopword": "^1.0.11",\n    "tailwindcss": "^2.2.7",\n    "eslint": "7.30.0",\n    "eslint-config-next": "11.0.1"\n  }\n}\n\n
Run Code Online (Sandbox Code Playgroud)\n

这是我的 Amplify.yaml 文件:

\n
version: 1\napplications:\n  - backend:\n      phases:\n        build:\n          commands:\n            - '# Execute Amplify CLI with the helper script'\n            - amplifyPush --simple\n    frontend:\n      phases:\n        preBuild:\n          commands:\n            - npm ci\n            - mkdir out\n        build:\n          commands:\n            - npm run build\n      artifacts:\n        baseDirectory: out\n        files:\n          - '**/*'\n      cache:\n        paths:\n          - node_modules/**/*\n    appRoot: slack-app\n\n
Run Code Online (Sandbox Code Playgroud)\n

任何帮助表示感谢,谢谢!

\n

编辑\n我只是想澄清一下,我能够在本地正常运行构建命令

\n