【问题标题】:I cant run my ReactJS app in Heroku我无法在 Heroku 中运行我的 ReactJS 应用程序
【发布时间】:2017-06-30 23:14:18
【问题描述】:

当我运行 npm start 时它可以工作,但在 heroku 中我无法做到,

2017-06-28T17:18:54.167693+00:00 app[web.1]: npm ERR! code ELIFECYCLE
2017-06-28T17:18:54.167918+00:00 app[web.1]: npm ERR! errno ENOENT
2017-06-28T17:18:54.168018+00:00 app[web.1]: npm ERR! syscall spawn
2017-06-28T17:18:54.168132+00:00 app[web.1]: npm ERR! juego-cartas-reactjs@0.1.0 start: `react-scripts start`
2017-06-28T17:18:54.168203+00:00 app[web.1]: npm ERR! spawn ENOENT
2017-06-28T17:18:54.168300+00:00 app[web.1]: npm ERR! 
2017-06-28T17:18:54.168387+00:00 app[web.1]: npm ERR! Failed at the juego-cartas-reactjs@0.1.0 start script.
2017-06-28T17:18:54.169487+00:00 app[web.1]: 
2017-06-28T17:18:54.168480+00:00 app[web.1]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2017-06-28T17:18:54.169649+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2017-06-28T17:18:54.169703+00:00 app[web.1]: npm ERR!     /app/.npm/_logs/2017-06-28T17_18_54_163Z-debug.log

我的 package.json,我在网上看到这可能是问题所在

<!-- language: lang-js -->

    {
      "name": "juego-cartas-reactjs",
      "version": "0.1.0",
      "main":"src/index.js",
      "dependencies": {
        "font-awesome": "^4.7.0",
        "lodash.shuffle": "^4.2.0",
        "react": "^15.6.1",
        "react-dom": "^15.6.1",
        "react-flipcard": "^0.2.1"
      },
      "devDependencies": {
        "react-scripts": "1.0.7"
      },

      "scripts": {
        "start": "react-scripts start",
        "build": "react-scripts build",
        "test": "react-scripts test --env=jsdom",
        "eject": "react-scripts eject"
      }
    }

我不知道该怎么办,如果有人对此有所了解,请帮助

【问题讨论】:

  • 您是否在服务器上运行了npm install 以确保react-scripts 存在?看起来这就是它失败的地方。
  • 你的意思是在我的 heroku 项目应用程序的控制台中?
  • 没错!查看their docs 以获得更多帮助。
  • 我认为这不是问题,因为我在我的应用程序中运行 npm start,它可以正常工作
  • 是的,因为您已在本地安装了所有依赖项。执行此操作,进入您的应用程序并删除 node_modules 文件夹。然后运行npm start,你应该得到同样的错误。然后,运行npm install,然后运行npm start,它应该会再次开始工作。无论应用在哪里运行,都需要下载并安装依赖项。

标签: javascript node.js reactjs heroku


【解决方案1】:

npm start用于本地开发。你不应该在 Heroku 上使用它。

将 Create React App 项目部署到 Heroku follow Heroku's official guide to it

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

本文链接自您创建的应用程序随附的 Deployment section of the User Guide。我建议在那里寻找可能的答案。

【讨论】:

  • 这实际上对我不起作用...当我尝试在 heroku 中运行它时应用程序仍然崩溃...
【解决方案2】:

我已经在 heroku 上成功上传了一个 create-react-app 项目,但我没有使用 create-react-app buildpack。这是我的设置:

package.json

//.. rest of package.json

"dependencies": {
  "express": "^4.14.0",
  //.. rest of dependencies 
},

// need to define engines for heroku
"engines": {
  "node": "6.9.1",
  "npm": "3.10.8"
},
"scripts": {
  "start": "node server.js #Production only!",
  "postinstall": "npm run build",
  "dev": "react-scripts start",
  "build": "react-scripts build",
  "test": "react-scripts test --env=jsdom",
  "eject": "react-scripts eject"
},

然后在项目的根目录中,我创建了一个 server.js,它只是一个非常基本的 express 应用程序。请注意,它使用 build 文件夹而不是 src 文件夹。

const express = require('express');
const path = require('path');

const app = express();
const port = process.env.PORT || 3000;

app.use(express.static(path.resolve(__dirname, 'build')));

app.get('*', (req, res) => {
  res.sendFile(path.resolve(__dirname, 'build', 'index.html'));
});

app.listen(port, () => {
  console.info(`Server listening on port ${port}`);
});

【讨论】:

    猜你喜欢
    • 2020-08-10
    • 2019-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多