【问题标题】:Rails API with Create-React-App on Heroku: npm command not found在 Heroku 上带有 Create-React-App 的 Rails API:找不到 npm 命令
【发布时间】:2017-02-08 00:08:57
【问题描述】:

我创建了 Rails API 后端应用程序,并在文件夹客户端的应用程序内为我的前端创建了 create-react-app。它在我的本地机器上运行良好,但在 heroku 上运行时出现错误:

npm: command not found

这是应用程序的结构:

rails-app
  - app
     - controller
     - model
     - ...
  - client
     - package.json
     - index.jsx
     - ...
  - config
  - db
  - ...
  - Procfile

在 Procfile 我有:

web: cd client && npm start
api: bundle exec rails s -p 3001

在 package.json 中:

{
  "name": "client",
  "version": "0.1.0",
  "private": true,
  "proxy": "http://localhost:3001/",
  "devDependencies": {
    "react-scripts": "0.6.1"
  },
  "dependencies": {
    "babel-loader": "^6.2.5",
    "babel-preset-es2015": "^6.14.0",
    "babel-preset-react": "^6.11.1",
    "firebase": "^3.4.1",
    "jquery": "^3.1.1",
    "react": "^15.3.2",
    "react-dom": "^15.3.2"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test --env=jsdom",
    "eject": "react-scripts eject"
  }
}

(注意与我的 Rails 后端通信的代理)

Heroku:

Heroku 日志:

2016-09-29T15:12:31.193617+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=strv-address-book.herokuapp.com request_id=bc33286a-3dd3-475f-b974-a00c5aee42f7 fwd="151.227.204.173" dyno= connect= service= status=503 bytes=
2016-09-29T15:12:44.403333+00:00 heroku[api.1]: Restarting
2016-09-29T15:12:44.404060+00:00 heroku[api.1]: State changed from up to starting
2016-09-29T15:12:45.064536+00:00 heroku[web.1]: State changed from crashed to starting
2016-09-29T15:12:47.345132+00:00 heroku[api.1]: Starting process with command `bundle exec rails s -p 3001`
2016-09-29T15:12:47.459080+00:00 heroku[web.1]: Starting process with command `cd client && npm start`
2016-09-29T15:12:47.931743+00:00 heroku[api.1]: State changed from starting to up
2016-09-29T15:12:48.531931+00:00 heroku[api.1]: Stopping all processes with SIGTERM
2016-09-29T15:12:49.283096+00:00 heroku[web.1]: State changed from starting to crashed
2016-09-29T15:12:49.263399+00:00 heroku[web.1]: Process exited with status 127
2016-09-29T15:12:49.036142+00:00 app[web.1]: bash: npm: command not found
2016-09-29T15:12:49.352458+00:00 app[api.1]: - Gracefully stopping, waiting for requests to finish
2016-09-29T15:12:49.352608+00:00 app[api.1]: === puma shutdown: 2016-09-29 15:12:49 +0000 ===
2016-09-29T15:12:49.352645+00:00 app[api.1]: - Goodbye!
2016-09-29T15:12:49.352737+00:00 app[api.1]: Exiting
2016-09-29T15:12:49.480691+00:00 heroku[api.1]: Process exited with status 0
2016-09-29T15:12:50.284874+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=strv-address-book.herokuapp.com request_id=c262f174-97c8-4326-a20b-3f13f2f6e99b fwd="151.227.204.173" dyno= connect= service= status=503 bytes=
2016-09-29T15:12:50.976612+00:00 app[api.1]: => Rails 5.0.0.1 application starting in production on http://0.0.0.0:3001
2016-09-29T15:12:50.976587+00:00 app[api.1]: => Booting Puma
2016-09-29T15:12:50.976617+00:00 app[api.1]: => Run `rails server -h` for more startup options
2016-09-29T15:12:51.938112+00:00 app[api.1]: Puma starting in single mode...
2016-09-29T15:12:51.938132+00:00 app[api.1]: * Version 3.6.0 (ruby 2.3.0-p0), codename: Sleepy Sunday Serenity
2016-09-29T15:12:51.938133+00:00 app[api.1]: * Min threads: 5, max threads: 5
2016-09-29T15:12:51.938134+00:00 app[api.1]: * Environment: production
2016-09-29T15:12:51.938225+00:00 app[api.1]: * Listening on tcp://0.0.0.0:3001
2016-09-29T15:12:51.938543+00:00 app[api.1]: Use Ctrl-C to stop
2016-09-29T15:12:52.380906+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=strv-address-book.herokuapp.com request_id=5d5f2cbb-4fd0-40f2-a8d4-86c2b8cf14f3 fwd="151.227.204.173" dyno= connect= service= status=503 bytes=

(我可以运行 heroku run rails c 没有任何错误)

如何向 heroku 告知 react 应用程序或如何使其工作?

【问题讨论】:

    标签: ruby-on-rails reactjs heroku rails-api create-react-app


    【解决方案1】:

    问题似乎是您的 Heroku 环境不知道如何运行 npm,这是可以理解的,因为默认情况下 Heroku 会推断出 buildpack 用于您的应用程序。

    您可以通过关注this 添加heroku-node buildpack。

    让我知道这是否有帮助或您是否需要进一步说明

    【讨论】:

    • 谢谢 oreoluwa。以这种方式设置 buildpacks:1.heroku/ruby,2.heroku/nodejs 并推送更改但无法部署:无法检测到 set buildpack codon-buildpacks.s3.amazonaws.com/buildpacks/heroku/nodejs.tgz remote。你知道怎么解决吗?我应该告诉 heroku 我的节点/反应应用程序在哪个文件夹中?
    • 尝试将您的 package.json 位置更改为应用程序的根目录 rails-app 并相应地更新您的脚本和 main。不完全有信心,但我想应该可以解决它
    • 此链接可以帮助您在 Heroku 上进行设置。medium.com/superhighfives/…
    猜你喜欢
    • 2018-01-09
    • 2019-04-26
    • 1970-01-01
    • 2018-01-25
    • 2017-06-06
    • 2019-08-17
    • 2017-02-02
    • 2018-04-25
    • 2022-10-01
    相关资源
    最近更新 更多