AWS Amplify中Gatsby预览构建偶发报错:无ID为1的Worker
问题
在AWS Amplify预览构建Gatsby项目时偶发失败(本地构建完全正常),核心报错为:
error UNHANDLED REJECTION There is no worker with "1" id.
完整报错日志:
2024-01-12T17:11:46.472Z [INFO]: success run queries in workers - 129.430s - 14/14 0.11/s 2024-01-12T17:11:56.660Z [WARNING]: error UNHANDLED REJECTION There is no worker with "1" id. 2024-01-12T17:11:56.728Z [INFO]: �[0m �[0m �[0m�[97m�[41mError�[0m�[37m�[41m:�[0m�[37m�[41m �[0m�[97m�[41mThere is no worker with "1" id.�[0m �[0m �[0m �[0m �[0m�[90m-�[0m �[0m�[93mindex.js�[0m�[90m:�[0m�[93m294�[0m�[37m �[0m�[37mWorkerPool.sendMessage�[0m �[0m �[0m �[0m�[90m[mokens-league-web]/[gatsby-worker]/dist/index.js:294:13�[0m �[0m �[0m �[0m �[0m�[90m-�[0m �[0m�[93mworker-messaging.ts�[0m�[90m:�[0m�[93m41�[0m�[37m �[0m �[0m �[0m �[0m�[90m[mokens-league-web]/[gatsby]/src/utils/jobs/worker-messaging.ts:41:22�[0m �[0m �[0m �[0m not finished Running gatsby-plugin-sharp.IMAGE_PROCESSING jobs - 230.677s 2024-01-12T17:11:57.980Z [ERROR]: !!! Build failed 2024-01-12T17:11:57.981Z [ERROR]: !!! Non-Zero Exit Code detected 2024-01-12T17:11:57.981Z [INFO]: # Starting environment caching...
环境配置
System: OS: Windows 10 10.0.22621 CPU: (12) x64 11th Gen Intel(R) Core(TM) i5-11400H @ 2.70GHz Binaries: Node: 18.17.1 - C:\Program Files\nodejs\node.EXE Yarn: 1.22.21 - ~\AppData\Roaming\npm\yarn.CMD npm: 9.6.7 - C:\Program Files\nodejs\npm.CMD Browsers: Edge: Chromium (120.0.2210.91) npmPackages: gatsby: ^4.2.0 => 4.25.7 gatsby-background-image: ^1.6.0 => 1.6.0 gatsby-plugin-anchor-links: ^1.2.1 => 1.2.1 gatsby-plugin-eslint: ^4.0.2 => 4.0.2 gatsby-plugin-google-gtag: ^4.11.0 => 4.25.0 gatsby-plugin-image: ^2.2.0 => 2.25.0 gatsby-plugin-manifest: ^4.2.0 => 4.25.0 gatsby-plugin-minify: ^0.5.0 => 0.5.0 gatsby-plugin-postcss: ^5.2.0 => 5.25.0 gatsby-plugin-react-helmet: ^5.4.0 => 5.25.0 gatsby-plugin-react-i18next: ^1.2.2 => 1.2.3 gatsby-plugin-root-import: ^2.0.9 => 2.0.9 gatsby-plugin-sass: ^5.2.0 => 5.25.0 gatsby-plugin-sharp: ^4.2.0 => 4.25.1 gatsby-plugin-sitemap: ^5.2.0 => 5.25.0 gatsby-source-filesystem: ^4.2.0 => 4.25.0 gatsby-source-s3: ^4.1.1 => 4.1.1 gatsby-transformer-sharp: ^4.2.0 => 4.25.0
Gatsby配置flags
flags: { FAST_DEV: true, PARALLEL_QUERY_RUNNING: true, },
已尝试方案:移除flags、构建脚本添加CPU_COUNT=1、构建前清理缓存、调整gatsby-images相关逻辑,均无效。
排查与解决建议
1. 锁定或调整Gatsby依赖版本
当前使用的Gatsby 4.25.7属于4.x分支后期版本,存在worker池相关偶发bug。可尝试:
- 降级到4.x稳定版本(如4.24.0)
- 升级到Gatsby 5.x(注意替换弃用插件,比如
gatsby-background-image需换成gatsby-plugin-image的背景图方案)
2. 调整Amplify构建资源与环境变量
Amplify预览构建的默认资源可能存在CPU/内存限制,导致worker进程被意外终止:
- 在Amplify控制台构建设置中选择更高配置的构建实例
- 构建命令前添加
GATSBY_WORKER_COUNT=1环境变量,强制单worker运行
3. 禁用图片处理并行化
报错关联gatsby-plugin-sharp.IMAGE_PROCESSING,在gatsby-config.js中配置sharp插件禁用并行:
module.exports = { plugins: [ { resolve: `gatsby-plugin-sharp`, options: { defaults: { formats: [`auto`, `webp`], placeholder: `dominantColor`, quality: 50, }, concurrency: 1, // 禁用并行处理 }, }, ], }
4. 检查S3数据源稳定性
项目使用gatsby-source-s3,若S3对象量大或网络不稳定,可能导致worker进程超时退出:
- 给
gatsby-source-s3添加超时配置 - 本地模拟Amplify网络环境,测试S3数据拉取是否偶发失败
5. 强制Amplify构建使用全新环境
在构建脚本中替换原有命令,确保每次构建无缓存残留:
rm -rf node_modules .cache public && yarn install && yarn build
内容的提问来源于stack exchange,提问作者Augusto Tobia
相关产品推荐
相关产品推荐

