You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在AWS Amplify部署Next.js时使用esbuild遇-rw-r--r--无效构建标志错误

解决AWS Amplify部署Next.js时esbuild的"Invalid build flag"错误

错误原因

你用ls -al命令获取文件列表时,输出包含了文件权限、所有者、文件大小等额外信息(比如-rw-r--r--),这些内容被当成esbuild的命令参数传入,导致报错。ls -al的输出不是纯文件路径,不能直接用于指定esbuild的处理文件。

修复步骤

1. 替换文件列表获取命令

把获取文件列表的命令改成只输出纯文件路径的方式,推荐用find命令,兼容性更好:

allfiles=$(find ./.next/standalone -name "*.js" -type f)

如果你的shell支持globstar(bash默认开启),也可以用:

allfiles=$(ls ./.next/standalone/**/*.js)

2. 执行压缩命令

修改后完整的构建命令序列:

allfiles=$(find ./.next/standalone -name "*.js" -type f)
npx esbuild $allfiles --minify --outdir=.next/standalone --platform=node --target=node16 --format=cjs --allow-overwrite

额外优化建议

  • 如果后续遇到权限问题,可以在压缩命令前添加权限调整命令,确保文件可读写:
    chmod -R 755 ./.next/standalone
    
  • 对于Next.js 12的standalone模式,也可以考虑在next.config.js中启用内置的压缩配置,减少手动处理的复杂度:
    module.exports = {
      compress: true,
      output: 'standalone',
    }
    

内容的提问来源于stack exchange,提问作者Hekmat

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.03 16:51:37