使用Webpack打包TypeScript代码后AWS Lambda出现Runtime.HandlerNotFound错误求助
I see exactly what's going on here—Webpack is wrapping your entire code in an IIFE (Immediately Invoked Function Expression) by default, which means your handler function isn't being exposed to Node.js's module system (i.e., it's not attached to module.exports). That's why Lambda can't find notifier.handler when you use the Webpack bundle, but it works fine with the raw TypeScript-compiled JS.
Let's fix this step by step:
1. Fix Your Webpack Configuration
First, your current Webpack config has a couple of critical issues:
- You're pointing the entry to
./src/notifier.jsinstead of the TypeScript source file - You haven't configured Webpack to output code compatible with Node.js's module system
Update your webpack.config.js to look like this:
const path = require('path'); module.exports = { target: 'node', // Critical: tells Webpack to target Node.js runtime entry: './src/notifier.ts', // Use the TypeScript file directly output: { filename: 'notifier.js', path: path.resolve(__dirname, 'dist'), libraryTarget: 'commonjs2', // Ensures exports are attached to module.exports }, resolve: { extensions: ['.ts', '.js'], // Lets Webpack resolve TypeScript files automatically }, module: { rules: [ { test: /\.ts$/, use: 'ts-loader', exclude: /node_modules/, }, ], }, };
2. Add ts-loader to Your Dependencies
Since we're now having Webpack handle TypeScript compilation directly, you need to install ts-loader (no more manual tsc command required!):
npm install --save-dev ts-loader
Your updated package.json devDependencies should include this new entry:
"devDependencies": { "@types/aws-lambda": "^8.10.84", "@types/node": "^16.11.6", "webpack-cli": "^4.9.1", "ts-loader": "^9.2.6" // Add this line }
3. Simplify Your Build Command
You no longer need to run tsc separately—Webpack will handle TypeScript compilation and bundling in one single step:
npx webpack --config webpack.config.js cd dist && zip notifier.zip notifier.js
4. Verify the Bundle Output
After rebuilding, check your dist/notifier.js file—you should see something like this at the end of the bundle:
module.exports = __webpack_exports__;
This confirms that your handler function is now properly exposed via module.exports, which Lambda's runtime can detect.
Bonus: Optimize Bundle Size
Since Lambda's environment already includes the AWS SDK, you can exclude it from your bundle to reduce its size. Install webpack-node-externals:
npm install --save-dev webpack-node-externals
Then update your Webpack config to add the externals rule:
const nodeExternals = require('webpack-node-externals'); module.exports = { // ... existing config externals: [nodeExternals()], // Excludes most node_modules from bundle (keeps Firebase) };
This will keep Firebase and your custom code in the bundle but skip dependencies that Lambda already provides out of the box.
Why This Works
target: 'node'ensures Webpack doesn't use browser-specific features and preserves Node.js module behaviorlibraryTarget: 'commonjs2'tells Webpack to export your code usingmodule.exports, which is exactly what Lambda expects for handler functions- Using
ts-loaderlets Webpack handle TypeScript compilation, so you don't have to manage separatetscand Webpack build steps
After making these changes, your Lambda should recognize the handler function without any issues.
内容的提问来源于stack exchange,提问作者eof

