【问题标题】:React app crashes on Heroku after using npm install使用 npm install 后,Heroku 上的 React 应用程序崩溃
【发布时间】:2017-06-30 02:11:05
【问题描述】:

每当我 npm install 任何模块时,我的 ReactJS 应用程序总是崩溃。

您可以通过在终端上执行此操作来重现问题。

create-react-app [项目名称]
cd [项目名称]
混帐初始化
heroku git:remote -a [heroku 应用程序的名称]
混帐添加。
git commit -m "init"
git push heroku master

您会发现一切正常,但请稍后再试。

npm install react-dom
(或任何包/模块。使用 --save 也会产生相同的错误)

git 添加 -u
git commit -m "新模块"
git push heroku 大师

应用程序会在服务器上崩溃,我不知道为什么。

注意:在这种情况下,react-dom 已经被 create-react-app 安装了,所以 npm 只会更新它。尽管如此,它还是崩溃了。您可以安装一个新模块,它的行为仍然相同。

根据要求,这里是错误日志。

2017-06-30T20:12:46.778184+00:00 app[web.1]: npm ERR! spawn ENOENT
2017-06-30T20:12:46.778396+00:00 app[web.1]: npm ERR! 
2017-06-30T20:12:46.778645+00:00 app[web.1]: npm ERR! Failed at the 007-test@0.1.0 start script.
2017-06-30T20:12:46.779136+00:00 app[web.1]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2017-06-30T20:12:46.780482+00:00 app[web.1]: 
2017-06-30T20:12:46.780763+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2017-06-30T20:12:46.780934+00:00 app[web.1]: npm ERR!     /app/.npm/_logs/2017-06-30T20_12_46_768Z-debug.log
2017-06-30T20:12:46.920375+00:00 heroku[web.1]: State changed from starting to crashed
2017-06-30T20:12:46.903436+00:00 heroku[web.1]: Process exited with status 1
2017-06-30T21:01:33.667285+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=react-app-007.herokuapp.com request_id=d4936b9c-4c21-4ce4-862c-8b05da3bc005 fwd="64.62.224.29" dyno= connect= service= status=503 bytes= protocol=https
2017-06-30T21:01:35.111452+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=react-app-007.herokuapp.com request_id=cef9f3f8-a46b-4760-9c24-de59ba540e2e fwd="64.62.224.29" dyno= connect= service= status=503 bytes= protocol=https

【问题讨论】:

  • 刚试过。没有成功:/
  • 你能发布 Heroku 错误日志的样子吗?他们可以在https://dashboard.heroku.com/apps/<your-project-id>/logs 找到。如果没有,请在与项目相同的目录中的命令行上运行heroku logs -n 500(日志中最近的 500 行应该足够了)
  • @Vongdarakia 看起来你尝试了所有这些步骤真的很快。有很多步骤,例如检查package.json文件中提到的节点版本,看看它是否与您的本地节点版本相同。你确定你都试过了吗?是的,请尝试@PatNeedham 的要求。我认为这也可能有所帮助。
  • @code_byter 我刚刚检查了版本。我本地的 npm 和 node 落后了,所以我更新了它们。我在本地运行应用程序仍然没有问题。

标签: node.js reactjs heroku deployment npm-install


【解决方案1】:

This sequence of commands should do the trick:

npm install -g create-react-app
create-react-app my-app
cd my-app
git init
heroku create -b https://github.com/mars/create-react-app-buildpack.git
git add .
git commit -m "react-create-app on Heroku"
git push heroku master
heroku open

我认为为 React (-b https://github.com/mars/create-react-app-buildpack.git) 添加自定义构建包是您所缺少的。在 Heroku 项目仪表板的设置选项卡下,您可以将构建包添加到现有项目中。

-b 标志是--buildpack 的缩写

$ heroku help create
Usage: heroku create [APP] [flags]

creates a new app

Flags:
 -b, --buildpack  buildpack url to use for this app
 -n, --no-remote  do not create a git remote
 -o, --org        organization to use
 -r, --remote     the git remote to create, default "heroku"
 -s, --stack      the stack to create the app on
 -t, --team       team to use
 --addons         comma-delimited list of addons to install
 --region         specify region for the app to run in
 --space          the private space to create the app in
 --ssh-git        use SSH git protocol for local git remote

这是我电脑上的 heroku cli 版本:

$ heroku --version
heroku-cli/6.12.0-a504409 (darwin-x64) node-v7.10.0

有关buildpacks 命令的特定信息:

$ heroku help buildpacks
Usage: heroku buildpacks [flags]

display the buildpack_url(s) for an app

Flags:
 -a, --app     (required) app to run command against
 -r, --remote  git remote of app to use

Examples:
    $ heroku buildpacks

heroku buildpacks commands: (get help with heroku help buildpacks:COMMAND)
 buildpacks               display the buildpack_url(s) for an app
 buildpacks:add URL       add new app buildpack, inserting into list of buildpacks if necessary
 buildpacks:clear         clear all buildpacks set on the app
 buildpacks:remove [URL]  remove a buildpack set on the app
 buildpacks:set URL       set new app buildpack, overwriting into list of buildpacks if necessary

您可以运行heroku buildpacks:add https://github.com/mars/create-react-app-buildpack.git,而不是通过网络仪表板添加该构建包。

【讨论】:

  • -b 标志的工作方式与 -a 标志相同,只是 -b 是创建一个新的 git,而 -a 是添加到现有的 git
  • @Vongdarakia -b 用于创建一个新的 git 分支,如果你像 git checkout -b my-new-branch 一样使用它。当用作heroku create 的标志时,它具有不同的上下文
  • 我明白了。我只是想出了我的问题。我会尽快发布答案。
【解决方案2】:

经过一番调试,我发现了根错误。当我 npm 安装一个包时,它与构建混淆了。模块 react-scripts 不再工作了。

我将它从 devDependencies 移到 package.json 中的依赖项,删除了我的节点模块 package-lock.json,并重新安装了所有内容,并且在本地工作。所以,我部署了它,现在一切正常。

【讨论】:

  • 请接受这个作为答案,以便对社区的其他人有所帮助! :) 谢谢
猜你喜欢
  • 2020-12-22
  • 1970-01-01
  • 1970-01-01
  • 2019-02-06
  • 2011-10-03
  • 2015-03-24
  • 2015-03-16
相关资源
最近更新 更多