AWS Lambda Node.js模块版本不匹配问题咨询
Let's break down what's happening here and how to fix it quickly.
The Root Cause
That error is telling you there's a mismatch between the Node.js ABI (Application Binary Interface) version of your compiled time module and the version Lambda is using:
- Node.js 6.10 uses ABI version 48 (which is what Lambda expects)
- The module you built was compiled for ABI version 46, which corresponds to Node.js 4.x
This means when you ran npm install time in your CodeBuild pipeline, it was using a Node.js 4.x environment by default—even though you set Lambda's runtime to 6.10. Native modules like time get compiled specifically for the Node.js version they're installed on, so this mismatch causes Lambda to throw that error.
The Fix: Align Build and Runtime Node.js Versions
You need to force CodeBuild to use Node.js 6.10 when installing your dependencies. Update your buildspec.yml to include commands that switch to the correct Node.js version before running npm install:
version: 0.1 phases: install: commands: # Switch to Node.js 6.10 using nvm (pre-installed in CodeBuild environments) - nvm install 6.10 - nvm use 6.10 # Verify the version to be safe - node --version # Now install dependencies with the correct Node.js version - npm install time # Package your SAM template as before - aws cloudformation package --template-file samTemplate.yaml --s3-bucket <redacted> --output-template-file NewSamTemplate.yaml artifacts: type: zip files: - NewSamTemplate.yaml
Why This Works
CodeBuild environments come pre-installed with nvm (Node Version Manager), which lets you easily switch between Node.js versions. By switching to 6.10 before installing time, the module gets compiled to match the ABI version Lambda expects (48), eliminating the mismatch error.
Bonus Tips
- Always ensure your build environment uses the exact same Node.js version as your Lambda runtime when working with native modules—this avoids version mismatches entirely.
- If you're testing locally, make sure your local Node.js version matches Lambda's too, so you can catch these issues before deploying.
内容的提问来源于stack exchange,提问作者user3131512

