发布react项目出错 --no-sandbox is not supported

发布react项目出错,错误信息如下

2024-06-24T02:14:36.960Z [WARNING]: Error: Failed to launch chrome!
ERROR:zygote_host_impl_linux.cc(89)] Running as root without --no-sandbox is not supported. See https://crbug.com/638180.TROUBLESHOOTING:https://github.com/GoogleChrome/puppeteer/blob/master/docs/troubleshooting.md
2024-06-24T02:14:36.976Z [WARNING]: error Command failed with exit code 1.
2024-06-24T02:14:36.976Z [INFO]: info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
2024-06-24T02:14:36.981Z [ERROR]: !!! Build failed
2024-06-24T02:14:36.981Z [ERROR]: !!! Error: Command failed with exit code 1

出现了 Puppeteer 启动 Chrome 浏览器的问题,特别是与 root 用户权限有关。Puppeteer 不支持以 root 用户身份运行除非使用 --no-sandbox 参数。这在 CI/CD 环境中是常见的问题。

你可以在项目根目录下使用 npm ls puppeteer 命令来检查是否安装了 Puppeteer:

npm ls puppeteer

解决方案

  1. 添加 --no-sandbox 参数:
    在 Puppeteer 的启动选项中添加 --no-sandbox 参数。
  2. 使用非 root 用户:
    配置 CodeBuild 使用非 root 用户,但这可能需要更多的配置。
  3. 在package.json文件中删除Puppeteer以及依赖的包(不使用的情况下)

在 amplify.yml 中添加 --no-sandbox 参数
假设你的构建步骤中涉及到 Puppeteer,可以在启动 Puppeteer 的地方添加 --no-sandbox 参数。

修改 Puppeteer 配置

const puppeteer = require('puppeteer');

(async () => {
  const browser = await puppeteer.launch({
    args: ['--no-sandbox', '--disable-setuid-sandbox']
  });
  // 其他代码
})();

更新 amplify.yml
确保你的 amplify.yml 文件中包含正确的安装和构建步骤。

version: 1
applications:
  - appRoot: . # 如果你的前端项目根目录是当前目录
    frontend:
      phases:
        preBuild:
          commands:
            - npm install # 或者 yarn install
        build:
          commands:
            - npm run build # 或者 yarn build
      artifacts:
        baseDirectory: build # 构建输出的目录,通常是build或dist
        files:
          - '**/*'
      cache:
        paths:
          - node_modules/**/*
    image: 'aws/codebuild/standard:5.0' # 使用最新的标准构建镜像

  • 11
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yunquantong

你的鼓励是我发布的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值