使用async/await导致AWS Lambda的Webpack构建失败,如何迁移至Node 8.10?
Nice question! I’ve dealt with this exact migration issue when moving my Webpack-built Lambda functions from Node.js 6.10 to 8.10—let me walk you through the fixes that worked for me:
1. Update Webpack Target to Node.js 8.10
Your old Webpack config might have targeted a generic node environment, which could be adding unnecessary transpilation or missing Node 8.10-specific optimizations. Update your webpack.config.js to explicitly set the target:
module.exports = { target: 'node8.10', // ... rest of your config };
This tells Webpack to optimize for Node 8.10's runtime, ensuring async/await and other ES2017 features work natively without extra overhead.
2. Tune Your Babel Config (If Used)
If you were using Babel to transpile code for Node 6.10, you can now dial back the transpilation since Node 8.10 supports async/await natively. Update your Babel preset (e.g., in .babelrc or babel.config.json):
{ "presets": [ ["@babel/preset-env", { "targets": { "node": "8.10" }, "useBuiltIns": "usage", "corejs": 3 }] ] }
Note: If you haven’t installed core-js yet, you’ll need to add it as a dependency—it helps fill in gaps for older Node APIs that might still be missing in 8.10.
3. Rewrite Lambda Handlers to Use Async/Await
Node 8.10’s Lambda runtime supports async functions directly, so you can ditch the old callback pattern entirely. Replace your handler code from:
exports.handler = (event, context, callback) => { // Your old callback-based logic callback(null, response); };
To:
exports.handler = async (event, context) => { // Your async/await logic here const response = await someAsyncTask(event); return response; };
Lambda will automatically handle the Promise returned by the async function, so no more manual callback calls!
4. Fix External Dependencies
Lambda’s Node 8.10 environment includes certain packages (like aws-sdk) by default. To avoid bloating your Webpack bundle, mark these as externals in your config:
module.exports = { // ... rest of config externals: { 'aws-sdk': 'aws-sdk' // Skip bundling AWS SDK since it's provided by Lambda } };
If you’re using webpack-node-externals to skip all node_modules, double-check it’s configured correctly—it should still work fine with Node 8.10, but it’s worth verifying:
const nodeExternals = require('webpack-node-externals'); module.exports = { externals: [nodeExternals()] };
5. Test Thoroughly
After updating your config and code, build your bundle and upload it to Lambda. Check CloudWatch Logs for any errors—common issues include:
- Missing modules: Verify your externals aren’t excluding packages you actually need to bundle
- Syntax errors: Ensure your Babel/Webpack targets are correctly set to Node 8.10 so async/await isn’t being transpiled incorrectly
内容的提问来源于stack exchange,提问作者Geek Stocks

