繁体   English   中英

错误:darwin-x64' 二进制文件不能在 'linux-x64' 平台上使用(AWS lambda + typescript + webpack 锐模块)

[英]Error: darwin-x64' binaries cannot be used on the 'linux-x64' platform (AWS lambda + typescript + webpack sharp module )

aws lambda 和 typescript 在由 webpack 捆绑时发生错误。

在捆绑 webpack 之前,我做了“npm i --arch=x64 --platform=linux --target=12.14.1 sharp”,labmda 工作正常。

但是,lambda上传zip尺寸越来越大。

所以,我想使用 serverless-webpack 调整 lambda 上传 zip 的大小。

Image lambda 除了使用夏普模块外工作良好。

我不知道该怎么做。

我做了

  1. 删除 node_modules 和 package-lock.json 并安装依赖项(也安装了sharp)
  2. 删除node_modules/sharp并安装sharp(lambda环境-linux,x64,节点版本)
  3. 在 serverless 中设置 serverless-webpack 配置:packagerOptions ( scrips ) - 重建锐利的 lambda 环境

但是,lambda 工作不正常。

我看了很多信息。

[λ linux 环境]
在 AWS Lambda function 中运行 Sharp 时出错:darwin-x64 二进制文件不能在“linux-x64”平台上使用

[无服务器-webpack]
https://github.com/serverless-heaven/serverless-webpack/issues/396

谢谢!


[编辑]

我的本地环境:Mac

生产环境:linux

也许,我认为带有“--platform”的 npm 命令在 mac 中不起作用。

此外,我使用 aws codebuild 解决了这个问题。

我发布了答案。

但是,它在我的本地 [Mac] 中不起作用

我通过告诉 webpack 在运行 npm 安装后重新安装 package 来使其工作:

webpack:
  includeModules:
    forceExclude:
      - aws-sdk
  packagerOptions:
    scripts:
      - rm -rf node_modules/sharp
      - npm install --arch=x64 --platform=linux sharp

我使用 aws codebuild 解决了这个问题。

codebuild 有 linux 和 node.js 运行时。

因此,我在 aws codebuild 中运行了以下命令( https://sharp.pixelplumbing.com/install

rm -rf node_modules/sharp
npm install --arch=x64 --platform=linux sharp

锋利的模块工作正常。

我通过使用 sam cli 使用此命令构建它来工作

sam build -u

此命令在与 lambda 具有相似环境的容器内构建代码

Dave Cowart 的回答对我帮助很大,但是在 repo 中有多个 lambda 函数,我不想在我的所有函数中安装Sharp。

这是我的解决方案:

serverless.yml

  webpack:
    webpackConfig: 'webpack.config.js'
    includeModules:
      forceExclude:
        - aws-sdk
    packagerOptions:
      scripts:
        - rm -rf node_modules/sharp
        - npm install --production --arch=x64 --platform=linux

您实际上不需要在安装脚本中指定Sharp。 Making it a generic npm install means it will only reinstall sharp if it's in the package.json (which it won't be if it's not used due to webpack tree shaking).

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM