Serverless部署AWS时Webpack无法解析模块问题求助
dynamodb-lib Context
I'm building a backend using the Serverless Framework, basing my work on a demo API repository. I kept the core config and resource files the same, but adjusted my directory structure to place handlers at controllers/service/handler.js, and updated the function definitions in serverless.yml to:
functions: - ${file(routes/user/user.routes.definitions.yml)}
Here's the full content of my user.routes.definitions.yml:
healthcheck: # Defines an HTTP API endpoint that calls the healthcheck function in handler.js # - path: url path is /health-check # - method: GET request handler: handler.healthcheck events: - http: path: health-check method: get cors: true create: handler: controllers/service/handler.main events: - http: path: users method: post cors: true authorizer: aws_iam get: # Defines an HTTP API endpoint that calls the main function in get.js # - path: url path is /users/{id} # - method: GET request handler: controllers/service/handler.main events: - http: path: users/{id} method: get cors: true authorizer: aws_iam
The Problem
When I run serverless deploy, I get consistent module-not-found errors for my dynamodb-lib and response-lib imports:
ERROR in ./controllers/user/get.js Module not found: Error: Can't resolve '../../../libs/dynamodb-lib' in 'E:\work\dittytv\backend\controllers\user' @ ./controllers/user/get.js 70:19-56 ERROR in ./controllers/user/list.js Module not found: Error: Can't resolve '../../../libs/dynamodb-lib' in 'E:\work\dittytv\backend\controllers\user' @ ./controllers/user/list.js 69:19-56 ERROR in ./controllers/user/create.js Module not found: Error: Can't resolve '../../../libs/dynamodb-lib' in 'E:\work\dittytv\backend\controllers\user' @ ./controllers/user/create.js 68:19-56
My controller files start with these import statements:
import * as dynamoDbLib from "../../../libs/dynamodb-lib"; import { success, failure } from "../../../libs/response-lib";
I've tried adjusting the paths to ./libs/, ./../../../libs, and other variations, but the error persists. I suspect the issue is with my Webpack configuration—here's what I'm using currently:
const slsw = require("serverless-webpack"); const nodeExternals = require("webpack-node-externals"); module.exports = { entry: slsw.lib.entries, target: "node", // Generate sourcemaps for proper error messages devtool: 'source-map', // Since 'aws-sdk' is not compatible with webpack, // we exclude all node dependencies externals: [nodeExternals()], mode: slsw.lib.webpack.isLocal ? "development" : "production", optimization: { // We no not want to minimize our code. minimize: false }, performance: { // Turn off size warnings for entry points hints: false }, // Run babel on all .js files and skip those in node_modules module: { rules: [ { test: /\.js$/, loader: "babel-loader", include: __dirname, exclude: /node_modules/ } ] } };
Could someone help me figure out what's causing this and how to fix it?
内容的提问来源于stack exchange,提问作者Harshit Laddha

