【问题标题】:Can't deploy React Project. Error is Command failed with exit code 1无法部署 React 项目。错误是命令失败,退出代码为 1
【发布时间】:2020-11-28 12:50:43
【问题描述】:

我编写了我的 React 项目,可以在 localhost 上运行,也可以在我的计算机上使用yarn build 命令进行构建。

我将我的项目推送到 github (Repo)。当我想将我的项目部署到 Vercel 或 Netlify 时,我收到了 Cannot find file '../../components/collection-item/Collection-item' in './src/pages/collection'. 错误,但路径或其他内容没有问题,正如我所说,我可以在自己的计算机上运行和构建它。我找不到解决方案。我更改了文件名和路径仍然相同。

这里是 Netlify 日志:

1:24:32 PM: build-image version: b0258b965567defc4a2d7e2f2dec2e00c8f73ad6
1:24:32 PM: build-image tag: v3.4.1
1:24:32 PM: buildbot version: bca3bbf1ce7c7ca2e2192b1683becc1d98c88c5b
1:24:32 PM: Fetching cached dependencies
1:24:32 PM: Failed to fetch cache, continuing with build
1:24:32 PM: Starting to prepare the repo for build
1:24:32 PM: No cached dependencies found. Cloning fresh repo
1:24:32 PM: git clone https://github.com/OrhanOzkercin/ReactShop
1:24:33 PM: Preparing Git Reference refs/heads/Build
1:24:34 PM: Different publish path detected, going to use the one specified in the Netlify configuration file: 'build' versus 'build/' in the Netlify UI
1:24:34 PM: Starting build script
1:24:34 PM: Installing dependencies
1:24:34 PM: Python version set to 2.7
1:24:35 PM: v12.18.0 is already installed.
1:24:35 PM: Now using node v12.18.0 (npm v6.14.4)
1:24:35 PM: Started restoring cached build plugins
1:24:35 PM: Finished restoring cached build plugins
1:24:36 PM: Attempting ruby version 2.7.1, read from environment
1:24:37 PM: Using ruby version 2.7.1
1:24:37 PM: Using PHP version 5.6
1:24:37 PM: 5.2 is already installed.
1:24:37 PM: Using Swift version 5.2
1:24:37 PM: Started restoring cached node modules
1:24:37 PM: Finished restoring cached node modules
1:24:37 PM: Started restoring cached yarn cache
1:24:37 PM: Finished restoring cached yarn cache
1:24:37 PM: Installing yarn at version 1.22.4
1:24:37 PM: Installing Yarn!
1:24:37 PM: > Downloading tarball...
1:24:37 PM: [1/2]: https://yarnpkg.com/downloads/1.22.4/yarn-v1.22.4.tar.gz --> /tmp/yarn.tar.gz.fZhfENjJTT
1:24:37 PM:   % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
1:24:37 PM:                                  Dload  Upload   Total   Spent    Left  Speed
1:24:37 PM:   0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
1:24:37 PM: 100    80  100    80    0     0    249      0 --:--:-- --:--:-- --:--:--   249
1:24:38 PM:   0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
1:24:38 PM: 100    93  100    93    0     0    139      0 --:--:-- --:--:-- --:--:--   332
1:24:38 PM: 100   630  100   630    0     0    612      0  0:00:01  0:00:01 --:--:--   612
1:24:39 PM:  22 1215k   22  270k    0     0   194k      0  0:00:06  0:00:01  0:00:05  194k
1:24:39 PM: 100 1215k  100 1215k    0     0   840k      0  0:00:01  0:00:01 --:--:-- 17.0M
1:24:39 PM: [2/2]: https://yarnpkg.com/downloads/1.22.4/yarn-v1.22.4.tar.gz.asc --> /tmp/yarn.tar.gz.fZhfENjJTT.asc
1:24:39 PM: 100    84  100    84    0     0   2358      0 --:--:-- --:--:-- --:--:--  2358
1:24:39 PM: 100    97  100    97    0     0   1433      0 --:--:-- --:--:-- --:--:--  1433
1:24:39 PM: 100   634  100   634    0     0   2905      0 --:--:-- --:--:-- --:--:--  2905
1:24:39 PM: 100  1028  100  1028    0     0   3986      0 --:--:-- --:--:-- --:--:--  3986
1:24:39 PM: > Verifying integrity...
1:24:39 PM: gpg: Signature made Mon 09 Mar 2020 03:52:13 PM UTC using RSA key ID 69475BAA
1:24:39 PM: gpg: Good signature from "Yarn Packaging <yarn@dan.cx>"
1:24:39 PM: gpg: WARNING: This key is not certified with a trusted signature!
1:24:39 PM: gpg:          There is no indication that the signature belongs to the owner.
1:24:39 PM: Primary key fingerprint: 72EC F46A 56B4 AD39 C907  BBB7 1646 B01B 86E5 0310
1:24:39 PM:      Subkey fingerprint: 6D98 490C 6F1A CDDD 448E  4595 4F77 6793 6947 5BAA
1:24:39 PM: > GPG signature looks good
1:24:39 PM: > Extracting to ~/.yarn...
1:24:39 PM: > Adding to $PATH...
1:24:39 PM: > Successfully installed Yarn 1.22.4! Please open another terminal where the `yarn` command will now be available.
1:24:40 PM: Installing NPM modules using Yarn version 1.22.4
1:24:40 PM: yarn install v1.22.4
1:24:40 PM: [1/4] Resolving packages...
1:24:41 PM: [2/4] Fetching packages...
1:25:03 PM: info fsevents@2.1.2: The platform "linux" is incompatible with this module.
1:25:03 PM: info "fsevents@2.1.2" is an optional dependency and failed compatibility check. Excluding it from installation.
1:25:03 PM: info fsevents@1.2.13: The platform "linux" is incompatible with this module.
1:25:03 PM: info "fsevents@1.2.13" is an optional dependency and failed compatibility check. Excluding it from installation.
1:25:03 PM: info fsevents@2.1.3: The platform "linux" is incompatible with this module.
1:25:03 PM: info "fsevents@2.1.3" is an optional dependency and failed compatibility check. Excluding it from installation.
1:25:03 PM: [3/4] Linking dependencies...
1:25:03 PM: warning " > @testing-library/user-event@7.2.1" has unmet peer dependency "@testing-library/dom@>=5".
1:25:03 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".
1:25:03 PM: warning " > styled-components@5.1.1" has unmet peer dependency "react-is@>= 16.8.0".
1:25:15 PM: [4/4] Building fresh packages...
1:25:17 PM: Done in 36.81s.
1:25:17 PM: NPM modules installed using Yarn
1:25:17 PM: Started restoring cached go cache
1:25:17 PM: Finished restoring cached go cache
1:25:17 PM: go version go1.14.4 linux/amd64
1:25:17 PM: go version go1.14.4 linux/amd64
1:25:17 PM: Installing missing commands
1:25:17 PM: Verify run directory
1:25:18 PM: ​
1:25:18 PM: ┌─────────────────────────────┐
1:25:18 PM: │        Netlify Build        │
1:25:18 PM: └─────────────────────────────┘
1:25:19 PM: ​
1:25:19 PM: ❯ Version
1:25:19 PM:   @netlify/build 3.1.10
1:25:19 PM: ​
1:25:19 PM: ❯ Flags
1:25:19 PM:   deployId: 5f2e7d5a88b3fd0007415022
1:25:19 PM:   mode: buildbot
1:25:19 PM:   timersFile: /tmp/substage_times.txt
1:25:19 PM: ​
1:25:19 PM: ❯ Current directory
1:25:19 PM:   /opt/build/repo
1:25:19 PM: ​
1:25:19 PM: ❯ Config file
1:25:19 PM:   No config file was defined: using default values.
1:25:19 PM: ​
1:25:19 PM: ❯ Context
1:25:19 PM:   production
1:25:19 PM: ​
1:25:19 PM: ┌───────────────────────────────────┐
1:25:19 PM: │ 1. Build command from Netlify app │
1:25:19 PM: └───────────────────────────────────┘
1:25:19 PM: ​
1:25:19 PM: $ yarn build
1:25:19 PM: yarn run v1.22.4
1:25:19 PM: $ react-scripts build
1:25:20 PM: Creating an optimized production build...
1:25:27 PM: Failed to compile.
1:25:27 PM: 
1:25:27 PM: ./src/pages/collection/Collectionpage.jsx
1:25:27 PM: Cannot find file '../../components/collection-item/Collection-item' in './src/pages/collection'.
1:25:28 PM: error Command failed with exit code 1.
1:25:28 PM: info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
1:25:28 PM:   
1:25:28 PM:   If the build failed with a warning about "process.env.CI = true", this is due to "create-react-app" treating warnings as errors when in CI. In order to fix this problem, please either:
1:25:28 PM:     - Fix the issues highlighted by the warnings above.
1:25:28 PM:     - Or modify the "scripts.build" command in your "package.json" from "react-scripts build" to "CI= react-scripts build"
1:25:28 PM:   More information can be found at https://docs.netlify.com/configure-builds/troubleshooting-tips/#build-fails-on-warning-message
1:25:28 PM: ​
1:25:28 PM: ┌─────────────────────────────┐
1:25:28 PM: │   "build.command" failed    │
1:25:28 PM: └─────────────────────────────┘
1:25:28 PM: ​
1:25:28 PM:   Error message
1:25:28 PM:   Command failed with exit code 1: yarn build
1:25:28 PM: ​
1:25:28 PM:   Error location
1:25:28 PM:   In Build command from Netlify app:
1:25:28 PM:   yarn build
1:25:28 PM: ​
1:25:28 PM:   Resolved config
1:25:28 PM:   build:
1:25:28 PM:     command: yarn build
1:25:28 PM:     commandOrigin: ui
1:25:28 PM:     publish: /opt/build/repo/build
1:25:28 PM: Caching artifacts
1:25:28 PM: Started saving node modules
1:25:28 PM: Finished saving node modules
1:25:28 PM: Started saving build plugins
1:25:28 PM: Finished saving build plugins
1:25:28 PM: Started saving yarn cache
1:25:28 PM: Finished saving yarn cache
1:25:28 PM: Started saving pip cache
1:25:28 PM: Finished saving pip cache
1:25:28 PM: Started saving emacs cask dependencies
1:25:28 PM: Finished saving emacs cask dependencies
1:25:28 PM: Started saving maven dependencies
1:25:28 PM: Finished saving maven dependencies
1:25:28 PM: Started saving boot dependencies
1:25:28 PM: Finished saving boot dependencies
1:25:28 PM: Started saving go dependencies
1:25:28 PM: Finished saving go dependencies
1:25:30 PM: Error running command: Build script returned non-zero exit code: 1
1:25:30 PM: Failing build: Failed to build site
1:25:30 PM: Failed during stage 'building site': Build script returned non-zero exit code: 1
1:25:30 PM: Finished processing build request in 58.952206659s```


 

【问题讨论】:

  • 您是否尝试在本地将import CollectionItem from ../../components/collection-item/Collection-item 更改为import CollectionItem from ../../components/collection-item/collection-item
  • 当然,但这不是文件的路径,这就是它抛出错误的原因。正如我所说,路径还可以,并且可以在我的计算机上运行。上传时出现问题。即使在我的计算机中,我也可以成功构建它。
  • 我克隆了你的项目,它在本地给出了同样的错误,在我将你的组件导入的字母从大写更改为非大写之后,它在本地工作,但不确定部署。
  • 在你告诉我我从 github 克隆项目并且我看到文件名不同之后发生了奇怪的事情。想法是,如果我想推送 git 时将文件名更改为大写版本,它会说 repo 是最新的。但我会深入研究。我认为这是关于推动 github。我之前没有意识到。非常感谢!
  • @OrhanÖzkerçin 我已经为您的问题提供了解决方案,请看一次,昨天我遇到了同样的问题并使用此解决方案解决了它。

标签: reactjs


【解决方案1】:

如果您在 netlify 上部署 React 应用程序,请使用以下方法

在基本构建设置中:-

  1. Base directory=(empty,如果在您正在运行的 repo 中,您可以直接访问组件)否则(根目录名称
  2. 构建命令 = (CI= npm run build)
  3. Publish Directory = (build) if no root directory or else (root-directory/build)

注意 - 确保检查 package.json 文件并在“脚本”中您应该有构建脚本,即“build”:“react-scripts build”,

【讨论】:

    【解决方案2】:

    在 netlify 中,

    在站点的部署设置下:
    将构建命令更改为 CI= npm run build

    应该是 构建命令:CI= npm run build

    【讨论】:

      【解决方案3】:
       CI= npm run build 
      
      • 站点设置 -> 构建和部署
      • 更改构建命令 CI= npm run
      • 构建重新部署您的网站

      【讨论】:

        【解决方案4】:

        您可以进入 package.json 和脚本中,将 build 替换为:

        "build": "CI= npm run build"
        

        您可以在他们的社区中阅读有关此错误原因的更多信息:

        https://answers.netlify.com/t/netlify-not-building-command-failed-with-exit-code-1-npm-run-build/16999/2

        【讨论】:

          【解决方案5】:

          为有同样问题的人解答。

          就我而言,我将一些文件名更改为大写版本。问题是 Git 没有解决这个更改,这就是我在 Github 上的文件结构错误的原因。所以解决方案是用 git 改变这个配置。

          git config core.ignorecase false 这个命令让 git 知道大小写。

          这是关于 Stacoverflow 的另一个问题:I change the capitalization of a directory and Git doesn't seem to pick up on it

          【讨论】:

          • 我的构建尝试推送到 firebase 失败,我没有意识到,但推送到 git 的文件大小写与我在本地实际拥有的文件大小写不同。
          【解决方案6】:

          进入 package.json 和脚本,将 build 替换为:

          "build": "CI= react-scripts build",
          

          【讨论】:

          • 这行得通。似乎在出现警告时构建失败。 CI 对此负责。更多可以找到here。搜索“将警告视为错误”
          猜你喜欢
          • 2018-10-15
          • 2018-07-12
          • 2015-10-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多