【问题标题】:Error: darwin-x64' binaries cannot be used on the 'linux-x64' platform (AWS lambda + typescript + webpack sharp module )错误:darwin-x64' 二进制文件不能在 'linux-x64' 平台上使用(AWS lambda + typescript + webpack sharp 模块)
【发布时间】:2020-09-14 06:08:26
【问题描述】:

通过 webpack 打包时,带有 typescript 的 aws lambda 发生错误。

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

但是,lambda 上传的 zip 大小越来越大。

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

Image lambda 除了使用 sharp 模块外运行良好。

我不知道该怎么做。

我做到了

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

但是,lambda 不能正常工作。

我看了很多资料。

[lambda linux 环境]
Error running Sharp inside AWS Lambda function: darwin-x64' binaries cannot be used on the 'linux-x64' platform

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

谢谢!


[编辑]

我的本​​地环境:Mac

生产环境:linux

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

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

我发布了答案。

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

【问题讨论】:

    标签: node.js webpack aws-lambda sharp


    【解决方案1】:

    我通过告诉 webpack 在运行 npm install 后重新安装包来让它工作:

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

    【讨论】:

    • 这样做时,它会增加所有功能。你有没有发现让它只绑定到使用Sharp的函数?
    • @AecioLevy 对我来说很幸运,对你来说很不幸,在我的特殊情况下,我只有一个功能。但是这个 GitHub 问题意味着不重写一堆无服务器代码是不容易的:github.com/serverless-heaven/serverless-webpack/issues/525
    • 非常感谢。我最终这样做了``` - if [ -f "api/resize.js" ];然后 rm -rf node_modules/sharp && npm install --arch=x64 --platform=linux sharp; fi``` 但无服务器组件似乎是一个更好的主意。
    【解决方案2】:

    我使用 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
    

    sharp 模块工作正常。

    【讨论】:

    【解决方案3】:

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

    sam build -u
    

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

    【讨论】:

      【解决方案4】:

      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。使它成为一个通用的 npm install 意味着它只会在 package.json 中重新安装Sharp(如果由于 webpack 树抖动而不使用它就不会)。

      【讨论】:

        猜你喜欢
        • 2020-05-27
        • 2022-09-25
        • 2021-10-19
        • 2020-01-15
        • 1970-01-01
        • 1970-01-01
        • 2021-08-31
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多