Next.js构建失败:aws-sdk相关Webpack错误排查求助
Next.js构建时AWS SDK相关Webpack错误解决方案
问题描述
构建Next.js项目时触发以下Webpack错误:
./node_modules/.pnpm/@aws-sdk+middleware-sdk-s3@3.624.0/node_modules/@aws-sdk/middleware-sdk-s3/dist-es/s3-express/functions/s3ExpressHttpSigningMiddleware.js + 33 modules Cannot get final name for export 'DEFAULT_MAX_ATTEMPTS' of ./node_modules/.pnpm/@smithy+util-retry@3.0.3/node_modules/@smithy/util-retry/dist-es/index.js > Build failed because of webpack errors ELIFECYCLE Command failed with exit code 1.
已尝试但无效的操作:
- 重置Next.js缓存
- 删除
node_modules和pnpm-lock.yaml后重装依赖 - 在
next.config.js中添加experimental: { serverComponentsExternalPackages: [ 'aws-sdk' ] }配置
项目完整依赖如下:
{ "name": "drippy_nextjs", "version": "0.1.0", "private": true, "scripts": { "dev": "next dev", "build": "next build", "start": "next start", "lint": "next lint", "postinstall": "prisma generate", "test": "jest", "test:watch": "jest --watch" }, "dependencies": { "@auth/prisma-adapter": "^1.0.11", "@aws-sdk/client-s3": "^3.624.0", "@aws-sdk/s3-presigned-post": "^3.624.0", "@aws-sdk/s3-request-presigner": "^3.624.0", "@googleapis/content": "^20.0.1", "@headlessui/react": "^1.7.18", "@heroicons/react": "^2.1.1", "@hookform/resolvers": "^3.3.4", "@next/bundle-analyzer": "^14.1.3", "@next/third-parties": "^14.1.3", "@prisma/client": "^5.8.0", "@radix-ui/react-accordion": "^1.1.2", "@radix-ui/react-aspect-ratio": "^1.0.3", "@radix-ui/react-avatar": "^1.0.4", "@radix-ui/react-checkbox": "^1.0.4", "@radix-ui/react-dialog": "^1.0.5", "@radix-ui/react-dropdown-menu": "^2.0.6", "@radix-ui/react-label": "^2.0.2", "@radix-ui/react-navigation-menu": "^1.1.4", "@radix-ui/react-progress": "^1.0.3", "@radix-ui/react-radio-group": "^1.1.3", "@radix-ui/react-scroll-area": "^1.0.5", "@radix-ui/react-select": "^2.0.0", "@radix-ui/react-separator": "^1.0.3", "@radix-ui/react-slot": "^1.0.2", "@radix-ui/react-tabs": "^1.0.4", "@radix-ui/react-toast": "^1.1.5", "@radix-ui/react-tooltip": "^1.0.7", "@smithy/util-retry": "^3.0.3", "@t3-oss/env-nextjs": "^0.9.2", "@tailwindcss/typography": "^0.5.10", "@tanstack/react-query": "^5.17.10", "@tanstack/react-table": "^8.11.7", "@trpc/client": "11.0.0-next-beta.193", "@trpc/next": "11.0.0-next-beta.193", "@trpc/react-query": "11.0.0-next-beta.193", "@trpc/server": "11.0.0-next-beta.193", "@types/crypto-js": "^4.2.2", "@types/express": "^4.17.21", "@types/heap": "^0.2.34", "@upstash/ratelimit": "^1.0.1", "@upstash/redis": "^1.28.3", "aws-sdk": "^2.1669.0", "axios": "^1.6.5", "bcrypt": "^5.1.1", "class-variance-authority": "^0.7.0", "clsx": "^2.1.0", "cmdk": "^0.2.0", "crypto-js": "^4.2.0", "date-fns": "^3.2.0", "dotenv": "^16.3.1", "embla-carousel-react": "8.0.0-rc19", "google-merchant-feed": "^0.1.0", "handlebars": "^4.7.8", "heap": "^0.2.7", "hex-color-to-color-name": "^1.0.2", "is-ua-webview": "^1.1.2", "keyword-extractor": "^0.0.28", "lucide-react": "^0.309.0", "next": "14.1.3", "next-auth": "^4.24.5", "nodemailer": "^6.9.8", "razorpay": "^2.9.2", "react": "^18", "react-beautiful-dnd": "^13.1.1", "react-dom": "^18", "react-hook-form": "^7.49.3", "react-icons": "^5.0.1", "react-intersection-observer": "^9.5.3", "react-parallax-tilt": "^1.7.212", "react-responsive-masonry": "^2.1.7", "react-sortable": "^2.0.0", "react-sortablejs": "^6.1.4", "react-swipeable": "^7.0.1", "react-toastify": "^10.0.4", "recharts": "^2.10.4", "schema-dts": "^1.1.2", "scrypt-async": "^2.0.1", "shortid": "^2.2.16", "slick-carousel": "^1.8.1", "superjson": "^2.2.1", "tailwind-merge": "^2.2.0", "tailwindcss-animate": "^1.0.7", "ts-node": "^10.9.2", "uuid": "^9.0.1", "vaul": "^0.8.0", "xlsx": "^0.18.5", "zod": "^3.22.4" }, "devDependencies": { "@tailwindcss/aspect-ratio": "^0.4.2", "@tailwindcss/forms": "^0.5.7", "@testing-library/jest-dom": "^6.4.2", "@testing-library/react": "^14.2.1", "@types/bcrypt": "^5.0.2", "@types/jest": "^29.5.12", "@types/node": "^20", "@types/nodemailer": "^6.4.14", "@types/react": "^18", "@types/react-beautiful-dnd": "^13.1.8", "@types/react-dom": "^18", "@types/react-responsive-masonry": "^2.1.3", "@types/react-slick": "^0.23.13", "@types/shortid": "^0.0.32", "@types/uuid": "^9.0.8", "autoprefixer": "^10.0.1", "eslint": "^8", "eslint-config-next": "14.0.4", "jest": "^29.7.0", "jest-environment-jsdom": "^29.7.0", "jest-mock-extended": "^3.0.5", "postcss": "^8", "prisma": "^5.8.0", "tailwindcss": "^3.3.0", "typescript": "^5" }, "prisma": { "seed": "ts-node --compiler-options {\"module\":\"CommonJS\"} prisma/seed.ts" } }
可行解决方案
方案1:移除冗余的AWS SDK v2依赖
package.json中同时安装了aws-sdk(v2)和@aws-sdk/*(v3),二者存在兼容性冲突,执行以下命令移除v2版本:
pnpm remove aws-sdk
之后重新执行构建命令。
方案2:调整Webpack外部依赖配置
在next.config.js中添加配置,将冲突的包标记为外部依赖,避免Webpack打包处理:
/** @type {import('next').NextConfig} */ const nextConfig = { webpack: (config) => { config.externals.push('@smithy/util-retry'); return config; }, }; module.exports = nextConfig;
方案3:修正实验性配置范围
之前的配置仅排除了v2版本的aws-sdk,改为明确排除v3相关包:
const nextConfig = { experimental: { serverComponentsExternalPackages: ['@aws-sdk/client-s3', '@aws-sdk/s3-presigned-post', '@aws-sdk/s3-request-presigner', '@smithy/util-retry'], }, }; module.exports = nextConfig;
方案4:降级AWS SDK v3版本
将@aws-sdk/*相关包降级到3.600.0左右的稳定版本,同时确保@smithy/util-retry版本与SDK兼容,修改package.json后重新安装依赖。
内容的提问来源于stack exchange,提问作者Rishabh
相关产品推荐
相关产品推荐

