Next.js 13项目中@aws-sdk包模块未找到问题求助
问题:Next.js API路由中无法解析
@aws-sdk/client-dynamodb包 问题描述
使用create-next-app创建全新项目后,在API路由中调用接口时,Next.js提示无法找到@aws-sdk/client-dynamodb包。已确认文件引用路径正确,尝试过更换路径、升级Next.js版本,但问题仍未解决。
错误信息
Module not found: Can't resolve '@aws-sdk/client-dynamodb'
相关API路由代码
import { NextResponse } from 'next/server' import { ddbDocClient } from "@/aws-services/dynamodb/ddbDocClient"; export async function POST(request: Request) { try { const { email, password } = await request.json(); if (!email || !password) { return NextResponse.json({ // 401 error: { code: 401, message: "Unauthorized", description: "Email and password are required" } }) } const response = await ddbDocClient.get({ TableName: "USERS", Key: { email } }); if (response?.Item?.password === password) { return NextResponse.json((response.Item as any)); // 200 } else { return NextResponse.json({ // 401 error: { code: 401, message: "Unauthorized", description: "Invalid email or password" } }) } } catch (err){ return NextResponse.json({ // 500 error: { code: 500, message: "Internal Server Error", description: "An error occurred while authorize user." } }) } }
已尝试的解决方法
- 更换文件引用路径
- 升级至最新版本的Next.js
补充配置文件
package.json
{ "name": "test-app", "version": "0.1.0", "private": true, "scripts": { "dev": "next dev", "build": "next build", "start": "next start", "lint": "next lint" }, "dependencies": { "@aws-sdk/client-dynamodb": "^3.395.0", "@aws-sdk/client-ses": "^3.395.0", "@aws-sdk/lib-dynamodb": "^3.395.0", "@types/node": "20.5.0", "@types/react": "18.2.20", "@types/react-dom": "18.2.7", "autoprefixer": "10.4.15", "aws-sdk": "^2.1440.0", "eslint": "8.47.0", "eslint-config-next": "13.4.17", "next": "^13.4.19", "next-auth": "^4.23.1", "postcss": "8.4.28", "react": "18.2.0", "react-dom": "18.2.0", "tailwindcss": "3.3.3", "typescript": "5.1.6" } }
tsconfig.json
{ "compilerOptions": { "target": "es5", "lib": ["dom", "dom.iterable", "esnext"], "allowJs": true, "skipLibCheck": true, "strict": true, "noEmit": true, "esModuleInterop": true, "module": "esnext", "moduleResolution": "bundler", "resolveJsonModule": true, "isolatedModules": true, "jsx": "preserve", "incremental": true, "plugins": [ { "name": "next" } ], "paths": { "@/*": ["./src/*"] } }, "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"], "exclude": ["node_modules"] }
可能的解决方法
1. 重新安装依赖包
依赖安装不完整或缓存损坏是常见原因,执行以下命令重置依赖:
# 删除依赖目录和锁文件 rm -rf node_modules package-lock.json yarn.lock # 重新安装依赖 npm install # 若使用yarn或pnpm,对应执行yarn install / pnpm install
2. 检查ddbDocClient内部的包引用
错误提示找不到@aws-sdk/client-dynamodb,大概率是你的@/aws-services/dynamodb/ddbDocClient文件中存在问题:
- 检查导入语句拼写:确认是
import { DynamoDBClient } from "@aws-sdk/client-dynamodb";,无大小写或拼写错误 - 确认未使用错误的相对路径或别名引用该包
3. 修改模块解析策略
Next.js 13+默认使用bundler模块解析,可能与AWS SDK存在兼容问题。修改tsconfig.json中的moduleResolution为node:
"moduleResolution": "node"
修改后重启开发服务器。
4. 强制转译AWS SDK包
Next.js的树摇机制可能误处理AWS SDK依赖,在next.config.js中添加转译配置:
/** @type {import('next').NextConfig} */ const nextConfig = { transpilePackages: ['@aws-sdk/client-dynamodb', '@aws-sdk/lib-dynamodb'], } module.exports = nextConfig
将相关AWS包加入转译列表,确保Next.js正确处理。
5. 确认项目根目录结构
确保package.json与node_modules在同一层级,避免嵌套项目结构导致的依赖查找失败。
内容的提问来源于stack exchange,提问作者Krystian
相关产品推荐
相关产品推荐

