添加API后AWS Amplify构建失败,报错npm.cmd ENOENT
AWS Amplify Angular应用添加API后构建失败:npm.cmd ENOENT错误
问题背景
我有一个部署在AWS Amplify上的Angular应用,原本构建、部署流程正常。但使用amplify add api命令添加API(关联新Lambda函数)并推送代码后,构建失败,报错信息如下:
Received error [Error: Command failed with ENOENT: npm.cmd install --no-bin-links --production spawn npm.cmd ENOENT] running command [npm.cmd install --no-bin-links --production]
已尝试重新配置项目、删除amplify文件夹后执行amplify pull --appId [appId] --envName [envName],问题仍未解决。
API配置详情
$ amplify add api √ Provide a friendly name for your resource to be used as a label for this category in the project: · api123 √ Provide a path (e.g., /book/{isbn}): · /items √ Choose a Lambda source · Create a new Lambda function ? Provide an AWS Lambda function name: checkout ? Choose the runtime that you want to use: NodeJS ? Choose the function template that you want to use: Serverless ExpressJS function (Integration with API Gateway) ✅ Available advanced settings: - Resource access permissions - Scheduled recurring invocation - Lambda layers configuration - Environment variables configuration - Secret values configuration ? Do you want to configure advanced settings? Yes ? Do you want to access other resources in this project from your Lambda function? No ? Do you want to invoke this function on a recurring schedule? No ? Do you want to enable Lambda layers for this function? No ? Do you want to configure environment variables for this function? No ? Do you want to configure secret values this function can access? Yes ? Enter a secret name (this is the key used to look up the secret value): secret_key ? Enter the value for secret_key: [hidden] ? What do you want to do? I'm done √ Choose the package manager that you want to use: · NPM Use the AWS SSM GetParameter API to retrieve secrets in your Lambda function. More information can be found here: https://docs.aws.amazon.com/systems-manager/latest/APIReference/API_GetParameter.html ? Do you want to edit the local lambda function now? No ✅ Successfully added resource checkout locally. ✅ Next steps: Check out sample function code generated in <project-dir>/amplify/backend/function/checkout/src "amplify function build" builds all of your functions currently in the project "amplify mock function <functionName>" runs your function locally To access AWS resources outside of this Amplify app, edit the \amplify\backend\function\checkout\custom-policies.json "amplify push" builds all of your local backend resources and provisions them in the cloud "amplify publish" builds all of your local backend and front-end resources (if you added hosting category) and provisions them in the cloud ✅ Succesfully added the Lambda function locally √ Restrict API access? (Y/n) · no √ Do you want to add another path? (y/N) · no ✅ Successfully added resource api123 locally
构建日志截图

解决方案
- 核对Lambda函数Node.js版本与Amplify构建环境版本:Amplify控制台构建设置中的Node.js版本需和Lambda配置的runtime版本一致,版本不匹配可能导致npm路径异常。
- 调整Lambda函数的包管理器配置:打开
amplify/backend/function/checkout/checkout-cloudformation-template.json,确认包管理器配置正确,也可尝试切换为Yarn(若环境支持)。 - 本地预构建Lambda函数:先在本地执行
amplify function build checkout,确保依赖安装无误后再推送代码,跳过云端重复安装步骤。 - 更新Amplify CLI到最新版:旧版CLI生成的配置可能与云端环境不兼容,执行
npm install -g @aws-amplify/cli更新后重新拉取配置并推送。 - 验证构建环境的npm可用性:在Amplify构建命令前添加
npm --version,确认npm正常可用,若不可用需手动指定路径。
内容的提问来源于stack exchange,提问作者Elliot Jimeno
相关产品推荐
相关产品推荐

