【问题标题】:Cannot find file on trying to deploy to heroku (works locally)在尝试部署到 heroku 时找不到文件(在本地工作)
【发布时间】:2019-06-02 19:17:54
【问题描述】:

我正在尝试将我的应用程序部署到 Heroku,它可以在本地运行,但不能在线运行。我删除并重新安装了节点模块。

我遇到了另一个与此非常相关的错误(文件未找到相同的名称等)我更改了相对路径,认为可以解决问题,但我没有得到任何结果

错误是:

找不到文件'./Components/SearchBar/SearchBar'

import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';
import SearchBar from './Components/SearchBar/SearchBar'
import BusinessList from './Components/BusinessList/BusinessList'
import Business from './Components/Business/Business'
import Yelp from './Components/Util/Yelp'

我希望不会因为如此简单的事情而出现错误,我已经倾注了文件和文件夹名称,但它没有任何意义。

PS 我认为它可能未连接我有 const yelpApiKey=process.env.yelpApiKey 让我的 heroku 连接到我的 API 密钥(在我的帐户中输入)

【问题讨论】:

  • 你的构建配置是什么?如果应用程序源指向“src”文件夹,则应防止从“src”文件夹外部导入。
  • 好的 - 我对 react 中的构建配置一无所知,我在哪里看?对于某些背景,我使用 create react app npm run build 给我带来了同样的错误。
  • 您可以尝试从导入中删除“src”吗?您要导入的当前文件的位置是什么?
  • 我添加了文件结构作为截图
  • 应该看起来像 import Business from './Components/Business/Business.js' 还是 import SearchBar from './Components/SearchBar/SearchBar.js' 因为我遇到了类似的错误就像那样,那是我更改文件路径以尝试反映根存在的时候。

标签: reactjs heroku deployment module


【解决方案1】:

Heroku 上的这个问题通常是由于你重命名了一个组件,例如将 MyComponent 重命名为 Mycomponent,而 git cache 和 heroku build cache 都可能出现这个问题。解决方案是:Failed to compile: Cannot find file './containers/App/App' in './src' 并清理 heroku 构建缓存以确保安全: https://help.heroku.com/18PI5RSY/how-do-i-clear-the-build-cache

【讨论】:

    【解决方案2】:

    在我的情况下,我在本地重命名了我的文件,但是当我将它推送到 github 上时,它并没有反映在那里。所以我删除了该文件并制作了具有相同内容的新文件并且它有效!

    【讨论】:

      【解决方案3】:

      我尝试了上述所有建议,但没有一个对我有用。最终,我所做的是删除了 heroku 抱怨丢失的文件,并将其替换为一个虚拟组件。之后,检查我对 heroku 的更改。现在一切都应该部署成功了。

      最后,我可以用我原来的组件换回我的旧虚拟组件并再次检查到 heroku 和哈利路亚,它工作了!!!!

      希望我的解决方法可以节省一些时间:)

      谢谢

      【讨论】:

        【解决方案4】:

        感谢@Jason 提出这个问题并感谢@Rakesh 的回答。

        我在 Heroku 上部署了 create-react-app 项目,然后我遇到了类似的错误,无法构建我的项目。错误是 routes.js 未能导入 signup.js

        Heroku 日志

        -----> Node.js app detected
               
        -----> Creating runtime environment
               
               NPM_CONFIG_LOGLEVEL=error
               NODE_ENV=production
               NODE_MODULES_CACHE=false
               NODE_VERBOSE=false
               
        -----> Installing binaries
               engines.node (package.json):  10.16.3
               engines.npm (package.json):   unspecified (use default)
               
               Resolving node version 10.16.3...
               Downloading and installing node 10.16.3...
               Using default npm version: 6.9.0
               
        -----> Restoring cache
               Caching has been disabled because NODE_MODULES_CACHE=false
               
        -----> Installing dependencies
               Installing node modules (package.json + package-lock)
               
               > core-js@2.6.9 postinstall /tmp/build_5d506b6dfc93b7f4b6575e02cc682130/node_modules/babel-runtime/node_modules/core-js
               > node scripts/postinstall || echo "ignore"
               
               
               > core-js@3.2.1 postinstall /tmp/build_5d506b6dfc93b7f4b6575e02cc682130/node_modules/core-js
               > node scripts/postinstall || echo "ignore"
               
               added 1507 packages from 719 contributors and audited 905071 packages in 35.746s
               found 0 vulnerabilities
               
               
        -----> Build
               Running build
               
               > chat-app@0.1.0 build /tmp/build_5d506b6dfc93b7f4b6575e02cc682130
               > react-scripts build
               
               Creating an optimized production build...
               Failed to compile.
               
               ./src/routes.js                                         // my beautiful error here
               Cannot find file './Components/User/signup/signup.js' in './src'.   
               
               
        npm ERR! code ELIFECYCLE
        npm ERR! errno 1
        npm ERR! chat-app@0.1.0 build: `react-scripts build`
        npm ERR! Exit status 1
        npm ERR! 
        npm ERR! Failed at the chat-app@0.1.0 build script.
        npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
        npm ERR! A complete log of this run can be found in:
        npm ERR!     /tmp/npmcache.ynm0W/_logs/2019-09-30T15_24_32_571Z-debug.log
        -----> Build failed
               
               We're sorry this build is failing! You can troubleshoot common issues here:
               https://devcenter.heroku.com/articles/troubleshooting-node-deploys
               
               If you're stuck, please submit a ticket so we can help:
               https://help.heroku.com/
               
               Love,
               Heroku
               
         !     Push rejected, failed to compile Node.js app.
         !     Push failed
        

        解决方案

        Heroku 在 Linux 服务器上部署项目,这意味着它是区分大小写的,如果你犯了错误,构建将失败。我建议在命名文件和目录时坚持使用小写。

        my project structure
        
        |root
        |src
        |  componets
        |     user
        |       signup
        |routes
        |index.js
        
        

        或者,

        在我改正错误后,我仍然有同样的错误。所以为什么? 我没有注意到 Git 没有推送更改。当你只重命名目录时,即使你做了 git addgit commitgit push,Git 有时也不会考虑这些更改。我建议更改后去网站验证。

        最后,

        部署到 Heroku 时,请仅部署基础分支。例如 Masterdevelop。我意识到每次部署我的功能分支时,Heroku 都会自动检测我的基础分支,即 develop

        【讨论】:

        • 你是救命恩人!我只花了 30 分钟弄清楚发生了什么
        • 这也为我节省了更多时间撕扯(剩下的)头发的时间!!谢谢!我不知道 Git 没有更新文件名的大小写更改!
        【解决方案5】:

        我发现您的代码有 2 个问题:

        1. 导入路径区分大小写(对于某些环境)。而您正在使用 'Components' 而不是 'components'
        2. index.js 中“应用”的导入错误。使用以下

          import App from './App';
          

        【讨论】:

        • 现在只是一个网站图标错误,我已经看过一百万次了。
        • 很奇怪。我讨厌这样说,但现在它在图标上崩溃了,即使我可以在浏览器中看到图标,它也不会播放
        • 是的,有趣的是我有一个 Rails 应用程序有这个问题(已修复),但我似乎无法弄清楚如何翻译它以做出反应。
        • 我删除了 heroku procfile,因为我遇到了一些与 Bin www 相关的错误,但当然 noheroku 上传了一个空白应用程序。
        • 可以确认这个问题,这是我的错误./src/App.js remote: Cannot find file './containers/UserPage' in './src'.我的导出名称是 Userpage,当我将文件从 UserPage 更改为 Userpage 时,修复了问题并成功部署了应用程序
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-02-09
        • 2020-01-09
        • 2014-08-25
        • 1970-01-01
        • 2019-09-27
        • 2023-03-19
        • 1970-01-01
        相关资源
        最近更新 更多