Netlify 使用 create-react-app (CRA) 网站构建错误

Netlify build Error using create-react-app (CRA) website

获取:构建脚本返回非零退出代码:1

我已经通过 Netlify 部署了我的 react js 网站代码,并得到了构建日志和错误如下:

10:37:15 AM: Build ready to start

10:37:16 AM: build-image version: 42bca793ccd33055023c56c4ca8510463a56d317

10:37:17 AM: buildbot version: 6bab8b64bbd90091082af19fedf16bf73d502e5e

10:37:17 AM: Fetching cached dependencies

10:37:17 AM: Failed to fetch cache, continuing with build

10:37:17 AM: Starting to prepare the repo for build

10:37:17 AM: No cached dependencies found. Cloning fresh repo

10:37:17 AM: git clone git@github.com:justtabhi/react-blog-exambunker

10:37:19 AM: Preparing Git Reference refs/heads/master

10:37:20 AM: Starting build script

10:37:20 AM: Installing dependencies

10:37:21 AM: Downloading and installing node v8.12.0...

10:37:21 AM: Downloading https://nodejs.org/dist/v8.12.0/node-v8.12.0-linux-x64.tar.xz...

10:37:21 AM: 
#

10:37:21 AM:                                        1.6%

10:37:22 AM: 
#############################

10:37:22 AM:                                             40.8%

10:37:22 AM: 
#####################################

10:37:22 AM: ################################### 100.0%

10:37:22 AM: Computing checksum with sha256sum

10:37:22 AM: Checksums matched!

10:37:24 AM: Now using node v8.12.0 (npm v6.4.1)

10:37:24 AM: Attempting ruby version 2.3.6, read from environment

10:37:25 AM: Using ruby version 2.3.6

10:37:26 AM: Using PHP version 5.6

10:37:26 AM: Started restoring cached node modules

10:37:26 AM: Finished restoring cached node modules

10:37:26 AM: Installing NPM modules using NPM version 6.4.1

10:37:53 AM: > uglifyjs-webpack-plugin@0.4.6 postinstall 
/opt/build/repo/node_modules/uglifyjs-webpack-plugin

10:37:53 AM: > node lib/post_install.js

10:37:55 AM: npm

10:37:55 AM: WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.4 (node_modules/fsevents):

10:37:55 AM: npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.4: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})

10:37:55 AM: added 1280 packages from 735 contributors and audited 12827 
packages in 28.118s

10:37:55 AM: found 0 vulnerabilities

10:37:55 AM: NPM modules installed

10:37:55 AM: Started restoring cached go cache

10:37:55 AM: Finished restoring cached go cache

10:37:55 AM: unset GOOS;

10:37:55 AM: unset GOARCH;

10:37:55 AM: export GOROOT='/opt/buildhome/.gimme/versions/go1.10.linux.amd64';

10:37:55 AM: export PATH="/opt/buildhome/.gimme/versions/go1.10.linux.amd64/bin:${PATH}";

10:37:55 AM: go version >&2;

10:37:55 AM: export GIMME_ENV='/opt/buildhome/.gimme/env/go1.10.linux.amd64.env';

10:37:55 AM: go version go1.10 linux/amd64

10:37:55 AM: Installing missing commands

10:37:55 AM: Verify run directory

10:37:55 AM: Executing user command: npm run build

10:37:55 AM: > react-blog@0.1.0 build /opt/build/repo

10:37:55 AM: > react-scripts build`enter code here`

10:37:57 AM: Creating an optimized production build...

10:38:02 AM: Failed to compile.

10:38:02 AM: Failed to minify the code from this file:

10:38:02 AM:    ./node_modules/indicative/builds/main.js:1:52673

10:38:02 AM: npm ERR! code ELIFECYCLE

10:38:02 AM: npm ERR! errno 1

10:38:02 AM: npm ERR! react-blog@0.1.0 build: `react-scripts build`

10:38:02 AM: npm ERR! Exit status 1

10:38:02 AM: npm ERR!

10:38:02 AM: npm ERR! Failed at the react-blog@0.1.0 build script.

10:38:02 AM: npm ERR! This is probably not a problem with npm. There is likely 
additional logging output above.

10:38:02 AM: npm ERR! A complete log of this run can be found in:

10:38:02 AM: npm ERR!     /opt/buildhome/.npm/_logs/2018-09-25T05_08_02_457Z-
debug.log

10:38:02 AM: Caching artifacts

10:38:02 AM: Started saving node modules

10:38:02 AM: Finished saving node modules

10:38:02 AM: Started saving pip cache

10:38:02 AM: Finished saving pip cache

10:38:02 AM: Started saving emacs cask dependencies

10:38:02 AM: Finished saving emacs cask dependencies

10:38:02 AM: Started saving maven dependencies

10:38:02 AM: Finished saving maven dependencies

10:38:02 AM: Started saving boot dependencies

10:38:02 AM: Finished saving boot dependencies

10:38:02 AM: Started saving go dependencies

10:38:02 AM: Finished saving go dependencies

10:38:03 AM: Cached node version v8.12.0

10:38:03 AM: Error running command: Build script returned non-zero exit code: 1

10:38:03 AM: Failing build: Failed to build site

10:38:03 AM: failed during stage 'building site': Build script returned non-zero 
exit code: 1

10:38:03 AM: Finished processing build request in 46.262986678s

请有人指出这里可能出了什么问题??

这里的问题是由于在您的 create-react-app 中使用 react-scripts 的构建环境引起的。您有一个正在导入的 node_modules 包,它的包格式不正确。

当 运行在 本地开发 中设置您的 npm run build 时,这是 Netlify 上发生的事情,您将在构建中看到以下错误:

Failed to minify the code from this file:

        ./node_modules/indicative/builds/main.js:1:52673

Read more here: http:// bit.ly/2tRViJ9

如link (http:// bit.ly/2tRViJ9)中所述,react-scripts需要升级到react-scripts@>=2.0.0

$ npm install react-scripts@^2.0.0

在以前的版本 (<2.0.0) 中,react-scripts 不支持 node_modules 中的 ES.Next 功能。它需要 CommonJS 或 ES,并建议为旧浏览器编译为 ES5。现在构建将允许您 设置 目标浏览器并使用 ES.Next 功能。

一旦您确认本地构建没有错误,您可以将这些更改推送到您的 git 存储库,您的 Netlify 构建也应该可以工作。

注意: 当使用 Netlify 作为您的 CI/CD 解决方案时,最好在本地开发之前确保 运行 您的构建命令将您的更改推送到主目标分支,以确保您的生产构建没有任何问题。