Netlify部署:在“构建站点”阶段失败:构建脚本返回非零退出代码:2。

31
我试图通过Github将我的ReactJS站点部署到Netlify,但是失败了。 错误消息如下: “构建站点”阶段失败:构建脚本返回非零退出代码:2 以下是完整的Netlify日志。
11:59:46 AM: Build ready to start
11:59:48 AM: build-image version: b0258b965567defc4a2d7e2f2dec2e00c8f73ad6
11:59:48 AM: build-image tag: v3.4.1
11:59:48 AM: buildbot version: 0a44c930054be748eff0c581d346282fece89cc0
11:59:48 AM: Building without cache
11:59:48 AM: Starting to prepare the repo for build
11:59:48 AM: No cached dependencies found. Cloning fresh repo
11:59:48 AM: git clone https://github.com/Helloiain/movie-app
11:59:49 AM: Preparing Git Reference refs/heads/master
11:59:50 AM: Starting build script
11:59:50 AM: Installing dependencies
11:59:50 AM: Python version set to 2.7
11:59:51 AM: v12.18.0 is already installed.
11:59:52 AM: Now using node v12.18.0 (npm v6.14.4)
11:59:52 AM: Started restoring cached build plugins
11:59:52 AM: Finished restoring cached build plugins
11:59:52 AM: Attempting ruby version 2.7.1, read from environment
11:59:53 AM: Using ruby version 2.7.1
11:59:54 AM: Using PHP version 5.6
11:59:54 AM: 5.2 is already installed.
11:59:54 AM: Using Swift version 5.2
11:59:54 AM: Started restoring cached node modules
11:59:54 AM: Finished restoring cached node modules
11:59:54 AM: Started restoring cached yarn cache
11:59:54 AM: Finished restoring cached yarn cache
11:59:54 AM: Installing yarn at version 1.22.4
11:59:54 AM: Installing Yarn!
11:59:54 AM: > Downloading tarball...
11:59:54 AM: [1/2]: https://yarnpkg.com/downloads/1.22.4/yarn-v1.22.4.tar.gz --> /tmp/yarn.tar.gz.aq7yGqZqlV
11:59:54 AM:   % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
11:59:54 AM:                                  Dload  Upload   Total   Spent    Left  Speed
11:59:54 AM:   0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
11:59:54 AM: 100    80  100    80    0     0    285      0 --:--:-- --:--:-- --:--:--   285
11:59:54 AM:   0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
11:59:54 AM: 100    93  100    93    0     0    156      0 --:--:-- --:--:-- --:--:--   556
11:59:55 AM: 100   643  100   643    0     0    749      0 --:--:-- --:--:-- --:--:--   749
11:59:55 AM: 100 1215k  100 1215k    0     0   947k      0  0:00:01  0:00:01 --:--:--  947k
11:59:55 AM: [2/2]: https://yarnpkg.com/downloads/1.22.4/yarn-v1.22.4.tar.gz.asc --> /tmp/yarn.tar.gz.aq7yGqZqlV.asc
11:59:55 AM: 100    84  100    84    0     0   3088      0 --:--:-- --:--:-- --:--:--  3088
11:59:55 AM: 100    97  100    97    0     0   1692      0 --:--:-- --:--:-- --:--:--  1692
11:59:55 AM: 100   647  100   647    0     0   7321      0 --:--:-- --:--:-- --:--:--  7321
11:59:55 AM:   0  1028    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
11:59:55 AM: 100  1028  100  1028    0     0   8135      0 --:--:-- --:--:-- --:--:-- 1003k
11:59:55 AM: > Verifying integrity...
11:59:55 AM: gpg: Signature made Mon 09 Mar 2020 03:52:13 PM UTC using RSA key ID 69475BAA
11:59:55 AM: gpg: Good signature from "Yarn Packaging <yarn@dan.cx>"
11:59:55 AM: gpg: WARNING: This key is not certified with a trusted signature!
11:59:55 AM: gpg:          There is no indication that the signature belongs to the owner.
11:59:55 AM: Primary key fingerprint: 72EC F46A 56B4 AD39 C907  BBB7 1646 B01B 86E5 0310
11:59:55 AM:      Subkey fingerprint: 6D98 490C 6F1A CDDD 448E  4595 4F77 6793 6947 5BAA
11:59:55 AM: > GPG signature looks good
11:59:55 AM: > Extracting to ~/.yarn...
11:59:55 AM: > Adding to $PATH...
11:59:56 AM: > Successfully installed Yarn 1.22.4! Please open another terminal where the `yarn` command will now be available.
11:59:56 AM: Installing NPM modules using Yarn version 1.22.4
11:59:57 AM: yarn install v1.22.4
11:59:57 AM: [1/4] Resolving packages...
11:59:58 AM: [2/4] Fetching packages...
12:00:22 PM: info fsevents@2.1.2: The platform "linux" is incompatible with this module.
12:00:22 PM: info "fsevents@2.1.2" is an optional dependency and failed compatibility check. Excluding it from installation.
12:00:22 PM: info fsevents@1.2.12: The platform "linux" is incompatible with this module.
12:00:22 PM: info "fsevents@1.2.12" is an optional dependency and failed compatibility check. Excluding it from installation.
12:00:22 PM: [3/4] Linking dependencies...
12:00:22 PM: warning " > @testing-library/user-event@7.2.1" has unmet peer dependency "@testing-library/dom@>=5".
12:00:22 PM: warning "react-scripts > @typescript-eslint/eslint-plugin > tsutils@3.17.1" has unmet peer dependency "typescript@>=2.8.0 || >= 3.2.0-dev || >= 3.3.0-dev || >= 3.4.0-dev || >= 3.5.0-dev || >= 3.6.0-dev || >= 3.6.0-beta || >= 3.7.0-dev || >= 3.7.0-beta".
12:00:34 PM: [4/4] Building fresh packages...
12:00:36 PM: success Saved lockfile.
12:00:36 PM: Done in 38.98s.
12:00:36 PM: NPM modules installed using Yarn
12:00:36 PM: Started restoring cached go cache
12:00:36 PM: Finished restoring cached go cache
12:00:36 PM: go version go1.14.4 linux/amd64
12:00:36 PM: go version go1.14.4 linux/amd64
12:00:36 PM: Installing missing commands
12:00:36 PM: Verify run directory
12:00:37 PM: ​
12:00:37 PM: ┌─────────────────────────────┐
12:00:37 PM: │        Netlify Build        │
12:00:37 PM: └─────────────────────────────┘
12:00:37 PM: ​
12:00:37 PM: ❯ Version
12:00:37 PM:   @netlify/build 5.0.2
12:00:37 PM: ​
12:00:37 PM: ❯ Flags
12:00:37 PM:   deployId: 5f9069020af24e00933f58d3
12:00:37 PM:   mode: buildbot
12:00:37 PM: ​
12:00:37 PM: ❯ Current directory
12:00:37 PM:   /opt/build/repo
12:00:37 PM: ​
12:00:37 PM: ❯ Config file
12:00:37 PM:   No config file was defined: using default values.
12:00:37 PM: ​
12:00:37 PM: ❯ Context
12:00:37 PM:   production
12:00:37 PM: ​
12:00:37 PM: ┌───────────────────────────────────┐
12:00:37 PM: │ 1. Build command from Netlify app │
12:00:37 PM: └───────────────────────────────────┘
12:00:37 PM: ​
12:00:37 PM: $ yarn build
12:00:38 PM: yarn run v1.22.4
12:00:38 PM: $ react-scripts build
12:00:39 PM: Creating an optimized production build...
12:00:41 PM: Browserslist: caniuse-lite is outdated. Please run the following command: `npx browserslist --update-db`
12:00:42 PM: Failed to compile.
12:00:42 PM: 
12:00:42 PM: ./src/index.js
12:00:42 PM: Cannot find file './app.js' in './src'.
12:00:42 PM: error Command failed with exit code 1.
12:00:42 PM: info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
12:00:42 PM: ​
12:00:42 PM: ┌─────────────────────────────┐
12:00:42 PM: │   "build.command" failed    │
12:00:42 PM: └─────────────────────────────┘
12:00:42 PM: ​
12:00:42 PM:   Error message
12:00:42 PM:   Command failed with exit code 1: yarn build
12:00:42 PM: ​
12:00:42 PM:   Error location
12:00:42 PM:   In Build command from Netlify app:
12:00:42 PM:   yarn build
12:00:42 PM: ​
12:00:42 PM:   Resolved config
12:00:42 PM:   build:
12:00:42 PM:     command: yarn build
12:00:42 PM:     commandOrigin: ui
12:00:42 PM:     publish: /opt/build/repo/build
12:00:42 PM: Caching artifacts
12:00:42 PM: Started saving node modules
12:00:42 PM: Finished saving node modules
12:00:42 PM: Started saving build plugins
12:00:42 PM: Finished saving build plugins
12:00:42 PM: Started saving yarn cache
12:00:42 PM: Finished saving yarn cache
12:00:42 PM: Started saving pip cache
12:00:42 PM: Finished saving pip cache
12:00:42 PM: Started saving emacs cask dependencies
12:00:42 PM: Finished saving emacs cask dependencies
12:00:42 PM: Started saving maven dependencies
12:00:42 PM: Finished saving maven dependencies
12:00:42 PM: Started saving boot dependencies
12:00:42 PM: Finished saving boot dependencies
12:00:42 PM: Started saving go dependencies
12:00:42 PM: Finished saving go dependencies
12:00:45 PM: Build failed due to a user error: Build script returned non-zero exit code: 2
12:00:45 PM: Failing build: Failed to build site
12:00:45 PM: Failed during stage 'building site': Build script returned non-zero exit code: 2
12:00:45 PM: Finished processing build request in 57.736891768s

在搜索了类似问题后,我认为可能是依赖性问题,但是在使用“Yarn”安装依赖项后,我仍然遇到了同样的问题。而使用“yarn build”进行本地构建则能正常工作。


是的,我不知道那是什么意思,app.js在其中。 - Ian
10个回答

68

在尝试将我的单页React JS网页应用程序部署到Netlify时,我遇到了相同的错误。

以下是我遵循的步骤来解决问题并成功部署网站的过程:

  1. 进入站点设置(Site Settings)

    Site Settings

  2. 从菜单中选择构建和部署(Build & Deploy)下拉菜单。

  3. 从下拉菜单中选择持续部署(Continuous Deployment)选项卡。

  4. 构建设置(Build Settings)部分,选择编辑设置(Edit settings)

    • 更改构建命令(build command)CI= npm run build
    • 点击保存(Save)

    Build Settings

  5. 然后从下拉菜单中选择环境(Environment)选项卡。

    • 选择编辑变量(Edit variables)
    • 键(Key)输入CI,将值(Value)输入false
    • 点击保存(Save)

    Environment Settings

  • 最后再次尝试部署网站。

  • 这将帮助您解决在Netlify中托管'Rect JS'应用程序的问题,您的应用程序将成功部署。


    2
    它能工作!所有的React应用都必须这样做吗?这甚至不在他们的文档中,Nelify不应该修复或补丁吗? - Jae Lee
    我认为这在他们的文档中有提到 @Jae Lee https://docs.netlify.com/configure-builds/troubleshooting-tips/#build-fails-on-warning-message - brienna
    我曾经遇到过同样的问题。它失败是因为 ESLint 失败了。你可以使用 CI=true npm run build 命令来运行 Netlify 运行的相同命令并查看失败情况。 - Devin Riegle
    它起作用了,虽然对我来说只需要第一步,即更改CI = npm run build。 - Yusuf Khan
    谢谢!它有效。@Pawara Siriwardhane - Tarique Anowar
    很高兴为您服务,@TariqueAnowar。 - Pawara Siriwardhane

    13

    按照如下确切步骤操作,因为我之前也遇到了同样的问题,后来采取了以下步骤才解决:

    1. 进入站点设置 > 构建和部署 > 持续部署
    2. 进入构建设置 > 编辑设置
    3. 构建命令修改为CI= npm run build(注意npm run build=之间的空格,一定要有)。
    4. 发布目录中填写build
    5. 进入环境 > 编辑变量
    6. 中输入变量CI,在中输入false
    7. 点击保存
    8. 进入部署,在第二个部分中,点击触发部署,选择清除缓存并部署站点。

    这样就可以解决该错误,并成功部署您的网站。祝您使用愉快!


    哈哈,CI=npm run build 没有空格把我搞笑了。 - Mystery Man
    将发布目录从默认的'dist'更改为'build'对我来说解决了问题,在尝试其他人提到的CI=和环境变量更改之后。 - pancake

    12

    您需要更改环境变量的值以便成功构建。请按照以下步骤进行:

    • 进入 部署设置
    • 单击 环境,然后单击 编辑变量 按钮。
    • 设置为CI,将设置为false
    • 现在单击保存按钮。

    现在,您可以在不出现错误的情况下构建和部署项目。


    1
    它对我起作用了,谢谢你哥们。 - Satish Kumar sonker
    它运行了!!! 感谢您的详细解释,您救了我的一天。 但我仍然不明白它是如何工作的。 - Sergio
    1
    @Sergio 你可以在空闲时间阅读这份文档。此外,你还可以通过谷歌搜索更多关于“环境变量”的内容。https://devcenter.heroku.com/articles/config-vars - Jaied

    2
    导航到Netlify上的项目,并在“环境”部分中选择“站点设置”页面,以解决Netlify的问题。
    添加两个新的环境变量后保存:
    1. CI的值为false 2. NPM_FLAGS的值为--legacy-peer-deps
    再次重新部署您的网站。

    1
    更改部署中的节点版本为16.x解决了我的问题。

    1
    你在Netlify部署过程中遇到的问题可能与构建环境中使用的Node.js版本有关。指定正确的Node.js版本可以解决此类问题。
    如何指定Node.js版本:
    在项目的根目录中创建一个`.nvmrc`文件。
    添加Node.js版本:考虑到许多现代包需要Node.js v14或更高版本,建议在`.nvmrc`文件中指定Node.js v14或v16。
    16
    

    下一次部署时,您应该能够看到Netlify从文件中获取您的Node版本。

    enter image description here


    0
    1. 进入网站配置 > 构建和部署 > 持续部署 > 构建设置 > 编辑(配置)。
    2. 将构建命令编辑为 CI= npm run build(确保有空格)。
    3. 进入环境变量 > 添加变量。添加键=CI,值=false。
    4. 尽管如此,我仍然遇到了相同的错误,所以我将更改我本地系统上安装的Node版本。
    5. 进入网站配置 > 构建和部署 > 持续部署 > 依赖管理 > 编辑(配置) > Node.js版本。[16.x] > 保存。
    6. 再次进入部署 > 触发部署 > 清除缓存并部署网站。

    0
    搞定了,问题在于 app.js 的大小写,我只需要将它改为 App.js 就好了。

    0

    我遇到了这个错误,因为我有一个编译错误,但我不知道,因为未提交的本地更改将其隐藏在我的本地环境中。

    在我的情况下,newFile.js 还没有被提交。但是 newFile.js 已经被导入到 committedFile.js 中。由于 newFile.js 没有被提交,构建失败了,无法编译可导入到 committedFile.js 中的文件。

    执行以下操作以查看是否存在隐藏的编译错误:

    使用 git stash -u 储藏本地未提交的更改,并查看是否在本地出现编译错误。


    0

    我也遇到了同样的错误,但是使用这个命令 CI= npm run build 后我的错误被清除了,但是后来我又遇到了同样的错误,然后尝试了这个方法,但是这次它不起作用了,但是我通过以下方法解决了问题:

    • 如果部署失败,请尝试还原推送并使用 CI= npm run build 再次尝试
    • 大多数错误是由于您安装的新版本或不支持的软件包(与 netlify 优化不匹配)引起的
    • 终端中将始终显示一个错误消息,请尝试识别导致错误的软件包-尝试删除或查询与 netlify 支持的版本

    发布新版本的软件包是一直在进行的,这给我在处理netlify方面带来了更多麻烦,所以我将我的部署平台切换到了aws-amplify;它非常简单,而且netlify导致的大多数错误在这里都没有发生。 - Dharan

    网页内容由stack overflow 提供, 点击上面的
    可以查看英文原文,
    原文链接