由于 gatsby.config 无效,无法使用 CloudFlare Pages 进行构建

Unable to use CloudFlare Pages to build due to gatsby.config invalid

我使用了cloudflare pages' to build my site, but I encountered the error: 无法读取未定义的属性(读取'split')`。以下是完整的cloudflare日志:

23:36:39.607    Initializing build environment. This may take up to a few minutes to complete
23:39:21.372    Success: Finished initializing build environment
23:39:21.372    Cloning repository...
23:39:23.083    Success: Finished cloning repository files
23:39:23.211    Installing dependencies
23:39:23.215    Python version set to 2.7
23:39:24.367    Downloading and installing node v17.4.0...
23:39:24.548    Downloading https://nodejs.org/dist/v17.4.0/node-v17.4.0-linux-x64.tar.xz...
23:39:24.860    Computing checksum with sha256sum
23:39:25.013    Checksums matched!
23:39:28.569    Now using node v17.4.0 (npm v8.3.1)
23:39:28.655    Started restoring cached build plugins
23:39:28.659    Finished restoring cached build plugins
23:39:28.808    Attempting ruby version 2.7.1, read from environment
23:39:29.966    Using ruby version 2.7.1
23:39:30.280    Using PHP version 5.6
23:39:30.317    5.2 is already installed.
23:39:30.325    Using Swift version 5.2
23:39:30.325    Started restoring cached node modules
23:39:30.328    Finished restoring cached node modules
23:39:31.135    Found npm version (8.3.1) that doesn't match expected (8.4.1)
23:39:31.135    Installing npm at version 8.4.1
23:39:34.201    
23:39:34.202    removed 1 package, changed 38 packages, and audited 217 packages in 3s
23:39:34.202    
23:39:34.202    11 packages are looking for funding
23:39:34.202      run `npm fund` for details
23:39:34.205    
23:39:34.205    3 moderate severity vulnerabilities
23:39:34.205    
23:39:34.205    To address all issues, run:
23:39:34.206      npm audit fix
23:39:34.206    
23:39:34.206    Run `npm audit` for details.
23:39:34.217    NPM installed successfully
23:39:34.658    Installing NPM modules using NPM version 8.4.1
23:39:51.138    npm WARN deprecated urix@0.1.0: Please see https://github.com/lydell/urix#deprecated
23:39:51.261    npm WARN deprecated resolve-url@0.2.1: https://github.com/lydell/resolve-url#deprecated
23:39:51.353    npm WARN deprecated source-map-resolve@0.5.3: See https://github.com/lydell/source-map-resolve#deprecated
23:39:51.996    npm WARN deprecated uuid@3.4.0: Please upgrade  to version 7 or higher.  Older versions may use Math.random() in certain circumstances, which is known to be problematic.  See https://v8.dev/blog/math-random for details.
23:39:53.095    npm WARN deprecated request@2.88.2: request has been deprecated, see https://github.com/request/request/issues/3142
23:39:54.180    npm WARN deprecated source-map-url@0.4.1: See https://github.com/lydell/source-map-url#deprecated
23:39:54.852    npm WARN deprecated querystring@0.2.1: The querystring API is considered Legacy. new code should use the URLSearchParams API instead.
23:39:59.626    npm WARN deprecated har-validator@5.1.5: this library is no longer supported
23:40:04.403    npm WARN deprecated babel-eslint@10.1.0: babel-eslint is now @babel/eslint-parser. This package will no longer receive updates.
23:40:05.052    npm WARN deprecated svgo@1.3.2: This SVGO version is no longer supported. Upgrade to v2.x.x.
23:40:06.272    npm WARN deprecated @hapi/bourne@1.3.2: This version has been deprecated and is no longer supported or maintained
23:40:06.971    npm WARN deprecated @hapi/address@2.1.4: Moved to 'npm install @sideway/address'
23:40:13.245    npm WARN deprecated @hapi/joi@15.1.1: Switch to 'npm install joi'
23:40:13.938    npm WARN deprecated @hapi/topo@3.1.6: This version has been deprecated and is no longer supported or maintained
23:40:14.138    npm WARN deprecated @hapi/hoek@8.5.1: This version has been deprecated and is no longer supported or maintained
23:40:14.754    npm WARN deprecated @hapi/hoek@8.5.1: This version has been deprecated and is no longer supported or maintained
23:43:07.275    
23:43:07.275    added 1961 packages, and audited 1962 packages in 4m
23:43:07.275    
23:43:07.275    272 packages are looking for funding
23:43:07.275      run `npm fund` for details
23:43:07.558    
23:43:07.558    43 vulnerabilities (20 moderate, 21 high, 2 critical)
23:43:07.558    
23:43:07.558    To address issues that do not require attention, run:
23:43:07.558      npm audit fix
23:43:07.558    
23:43:07.558    To address all issues (including breaking changes), run:
23:43:07.558      npm audit fix --force
23:43:07.558    
23:43:07.558    Run `npm audit` for details.
23:43:07.581    NPM modules installed
23:43:08.028    Installing Hugo 0.54.0
23:43:08.672    Hugo Static Site Generator v0.54.0-B1A82C61A/extended linux/amd64 BuildDate: 2019-02-01T10:04:38Z
23:43:08.674    Started restoring cached go cache
23:43:08.683    Finished restoring cached go cache
23:43:08.763    go version go1.14.4 linux/amd64
23:43:08.770    go version go1.14.4 linux/amd64
23:43:08.771    Installing missing commands
23:43:08.771    Verify run directory
23:43:08.771    Executing user command: gatsby build
23:43:09.178    tput: unknown terminal "unknown"
23:43:09.180    ╔════════════════════════════════════════════════════════════════════════╗
23:43:09.180    ║                                                                        ║
23:43:09.180    ║   Gatsby collects anonymous usage analytics                            ║
23:43:09.180    ║   to help improve Gatsby for all users.                                ║
23:43:09.180    ║                                                                        ║
23:43:09.180    ║   If you'd like to opt-out, you can use `gatsby telemetry --disable`   ║
23:43:09.180    ║   To learn more, checkout https://gatsby.dev/telemetry                 ║
23:43:09.180    ║                                                                        ║
23:43:09.180    ╚════════════════════════════════════════════════════════════════════════╝
23:43:12.022    
23:43:12.022     ERROR #10123  CONFIG
23:43:12.023    
23:43:12.023    We encountered an error while trying to load your site's gatsby-config. Please
23:43:12.023    fix the error and try again.
23:43:12.023    
23:43:12.023    
23:43:12.088    [2K[1A[2K[G
23:43:12.089    [0m
23:43:12.089    [0m  [0m[97m[41mTypeError[0m[37m[41m:[0m[37m[41m [0m[97m[41mCannot read properties of undefined (reading 'split')[0m
23:43:12.089    [0m  [0m
23:43:12.089    [0m  [0m[90m-[0m [0m[93mgatsby-config.js[0m[90m:[0m[93m30[0m[37m [0m[37mObject.<anonymous>[0m
23:43:12.089    [0m  [0m  [0m[90m/opt/buildhome/repo/gatsby-config.js:30:67[0m
23:43:12.089    [0m  [0m
23:43:12.089    [0m  [0m[90m-[0m [0m[93mv8-compile-cache.js[0m[90m:[0m[93m192[0m[37m [0m[37mModule._compile[0m
23:43:12.089    [0m  [0m  [0m[90m[repo]/[v8-compile-cache]/v8-compile-cache.js:192:30[0m
23:43:12.089    [0m  [0m
23:43:12.089    [0m  [0m[90m-[0m [0m[93mloader[0m[90m:[0m[93m1149[0m[37m [0m[37mObject.Module._extensions..js[0m
23:43:12.089    [0m  [0m  [0m[90mnode:internal/modules/cjs/loader:1149:10[0m
23:43:12.089    [0m  [0m
23:43:12.089    [0m  [0m[90m-[0m [0m[93mloader[0m[90m:[0m[93m975[0m[37m [0m[37mModule.load[0m
23:43:12.089    [0m  [0m  [0m[90mnode:internal/modules/cjs/loader:975:32[0m
23:43:12.089    [0m  [0m
23:43:12.089    [0m  [0m[90m-[0m [0m[93mloader[0m[90m:[0m[93m822[0m[37m [0m[37mFunction.Module._load[0m
23:43:12.089    [0m  [0m  [0m[90mnode:internal/modules/cjs/loader:822:12[0m
23:43:12.089    [0m  [0m
23:43:12.089    [0m  [0m[90m-[0m [0m[93mloader[0m[90m:[0m[93m999[0m[37m [0m[37mModule.require[0m
23:43:12.089    [0m  [0m  [0m[90mnode:internal/modules/cjs/loader:999:19[0m
23:43:12.089    [0m  [0m
23:43:12.089    [0m  [0m[90m-[0m [0m[93mv8-compile-cache.js[0m[90m:[0m[93m159[0m[37m [0m[37mrequire[0m
23:43:12.089    [0m  [0m  [0m[90m[repo]/[v8-compile-cache]/v8-compile-cache.js:159:20[0m
23:43:12.089    [0m  [0m
23:43:12.089    [0m  [0m[90m-[0m [0m[93mget-config-file.ts[0m[90m:[0m[93m30[0m[37m [0m[37mgetConfigFile[0m
23:43:12.089    [0m  [0m  [0m[90m[repo]/[gatsby]/src/bootstrap/get-config-file.ts:30:20[0m
23:43:12.089    [0m  [0m
23:43:12.089    [0m  [0m[90m-[0m [0m[93mload-config-and-plugins.ts[0m[90m:[0m[93m28[0m[37m [0m[37mloadConfigAndPlugins[0m
23:43:12.089    [0m  [0m  [0m[90m[repo]/[gatsby]/src/bootstrap/load-config-and-plugins.ts:28:50[0m
23:43:12.089    [0m  [0m
23:43:12.089    [0m  [0m[90m-[0m [0m[93minitialize.ts[0m[90m:[0m[93m146[0m[37m [0m[37minitialize[0m
23:43:12.089    [0m  [0m  [0m[90m[repo]/[gatsby]/src/services/initialize.ts:146:46[0m
23:43:12.089    [0m  [0m
23:43:12.089    [0m  [0m[90m-[0m [0m[93mindex.ts[0m[90m:[0m[93m47[0m[37m [0m[37mbootstrap[0m
23:43:12.089    [0m  [0m  [0m[90m[repo]/[gatsby]/src/bootstrap/index.ts:47:15[0m
23:43:12.089    [0m  [0m
23:43:12.089    [0m  [0m[90m-[0m [0m[93mbuild.ts[0m[90m:[0m[93m94[0m[37m [0m[37mbuild[0m
23:43:12.089    [0m  [0m  [0m[90m[repo]/[gatsby]/src/commands/build.ts:94:59[0m
23:43:12.089    [0m  [0m
23:43:12.089    [0m
23:43:12.089    
23:43:12.095    [2K[1A[2K[Gnot finished open and validate gatsby-configs, load plugins - 0.188s
23:43:12.095    
23:43:12.285    Failed: build command exited with code: 1

奇怪的是local可以build成功,cloudflare却不行。我尝试为 cloudflare 指定相同的 nodenpmgatsbygatsby-cli 版本作为本地,但仍然出现此错误。

具体来说,在 CloudFlare Pages 上,我创建了两个环境变量来指定 NodeNPM versions.They 是:

变量名 | 价值
NODE_VERSION17.4.0
NPM_VERSION8.4.1

从日志中可以看出,他们的版本已经指定成功了。对于gatsbygatsby-cli,我将它们的版本分别指定为3.14.64.6.1,在package.json.

中更改了

不幸的是,正如我之前所说,本地构建工作正常,但 cloudflare 仍然抛出 Cannot read properties of undefined (reading 'split') 错误。问题是什么?有什么解决办法吗?

我猜你对 configure languages Kontent gatsby source plugin as it is in starters 使用了相同的方法。

根据错误,我猜你的环境变量没有传播到 Cloudflare。所以 process.env.KONTENT_LANGUAGE_CODENAMESundefined -> 这会导致 gatsby-config.js:30:67.

上的错误 cannot read properties of undefined (reading 'split')