【问题标题】:Heroku with React and nodemailer build failingHeroku 与 React 和 nodemailer 构建失败
【发布时间】:2018-11-23 01:48:33
【问题描述】:

让我在 heroku 上的构建工作时遇到问题。 Heroku 说构建成功,但是当我打开页面时,我得到了应用程序错误页面。

这是我用于 React 的 package.JSON

{
  "name": "cloudveilheli",
  "version": "0.1.0",
  "private": true,
  "proxy": "http://localhost:3001",
  "dependencies": {
    "axios": "^0.18.0",
    "body-parser": "^1.18.3",
    "concurrently": "^3.5.1",
    "express": "^4.16.3",
    "firebase": "^4.8.1",
    "moment": "^2.22.1",
    "nodemailer": "^4.6.5",
    "nodemon": "^1.17.5",
    "re-base": "^3.2.1",
    "react": "^16.2.0",
    "react-dom": "^16.2.0",
    "react-router": "^4.2.0",
    "react-router-dom": "^4.2.2"
  },
  "devDependencies": {
    "node-sass": "^4.7.2",
    "react-scripts": "1.0.17",
    "redux": "^3.7.2",
    "sass-loader": "^6.0.6",
    "webpack": "^3.10.0"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test --env=jsdom",
    "eject": "react-scripts eject"
  }
}

对于 node.js

{
  "name": "cloudveilheli",
  "version": "0.1.0",
  "private": true,
  "proxy": "http://localhost:3001",
  "dependencies": {
    "axios": "^0.18.0",
    "body-parser": "^1.18.3",
    "concurrently": "^3.5.1",
    "express": "^4.16.3",
    "firebase": "^4.8.1",
    "moment": "^2.22.1",
    "nodemailer": "^4.6.5",
    "nodemon": "^1.17.5",
    "re-base": "^3.2.1",
    "react": "^16.2.0",
    "react-dom": "^16.2.0",
    "react-router": "^4.2.0",
    "react-router-dom": "^4.2.2"
  },
  "devDependencies": {
    "node-sass": "^4.7.2",
    "react-scripts": "1.0.17",
    "redux": "^3.7.2",
    "sass-loader": "^6.0.6",
    "webpack": "^3.10.0"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test --env=jsdom",
    "eject": "react-scripts eject"
  }
}

在日志中,我收到一个错误,即 react-scripts 在 Heroku 构建过程中失败。节点和反应被解雇,但很快就失败了。

这里是heroku日志

2018-06-13T12:56:17.879178+00:00 app[web.1]: [1] sh: 1: react-scripts: not found
2018-06-13T12:56:17.910185+00:00 app[web.1]: [1] npm ERR! file sh
2018-06-13T12:56:17.910189+00:00 app[web.1]: [1] npm ERR! code ELIFECYCLE
2018-06-13T12:56:17.912160+00:00 app[web.1]: [1] npm ERR! errno ENOENT
2018-06-13T12:56:17.913985+00:00 app[web.1]: [1] npm ERR! syscall spawn
2018-06-13T12:56:17.922554+00:00 app[web.1]: [1] npm ERR! cloudveilheli@0.1.0 start: `react-scripts start`
2018-06-13T12:56:17.927487+00:00 app[web.1]: [1] npm ERR! spawn ENOENT
2018-06-13T12:56:17.928818+00:00 app[web.1]: [1] npm ERR!
2018-06-13T12:56:17.932398+00:00 app[web.1]: [1] npm ERR! Failed at the cloudveilheli@0.1.0 start script.
2018-06-13T12:56:17.932401+00:00 app[web.1]: [1] npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2018-06-13T12:56:17.967183+00:00 app[web.1]: [1] npm WARN Local package.json exists, but node_modules missing, did you mean to install?
2018-06-13T12:56:17.968219+00:00 app[web.1]: [1]
2018-06-13T12:56:17.968222+00:00 app[web.1]: [1] npm ERR! A complete log of this run can be found in:
2018-06-13T12:56:17.968224+00:00 app[web.1]: [1] npm ERR!     /app/.npm/_logs/2018-06-13T12_56_17_935Z-debug.log
2018-06-13T12:56:17.992492+00:00 app[web.1]: [1] npm ERR! code ELIFECYCLE
2018-06-13T12:56:17.992495+00:00 app[web.1]: [1] npm ERR! errno 1
2018-06-13T12:56:17.996672+00:00 app[web.1]: [1] npm ERR! bluebirdheliapp@1.0.0 client: `npm run start --prefix cloudveil_heli_app`
2018-06-13T12:56:17.999397+00:00 app[web.1]: [1] npm ERR! Exit status 1
2018-06-13T12:56:18.004427+00:00 app[web.1]: [1] npm ERR!
2018-06-13T12:56:18.004430+00:00 app[web.1]: [1] npm ERR! Failed at the bluebirdheliapp@1.0.0 client script.
2018-06-13T12:56:18.004432+00:00 app[web.1]: [1] npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2018-06-13T12:56:18.021046+00:00 app[web.1]: [1]
2018-06-13T12:56:18.021050+00:00 app[web.1]: [1] npm ERR! A complete log of this run can be found in:
2018-06-13T12:56:18.021052+00:00 app[web.1]: [1] npm ERR!     /app/.npm/_logs/2018-06-13T12_56_18_005Z-debug.log
2018-06-13T12:56:18.046560+00:00 app[web.1]: [1] npm run client exited with code 1

【问题讨论】:

    标签: node.js reactjs heroku npm


    【解决方案1】:

    当你将你的应用推送到 Heroku 时

    首先运行 npm install

    稍后它会运行 npm start

    所以我们必须在 package.json 中声明 npm install 完成后要运行什么以及 npm start 需要做什么。

    更新你的 package.json:

        {
          "name": "cloudveilheli",
          "version": "0.1.0",
          "private": true,
          "proxy": "http://localhost:3001",
    "scripts": {
        "start": "node server.js",
        "postinstall": "npm start build "
      },
          "dependencies": {
            "axios": "^0.18.0",
            "body-parser": "^1.18.3",
    

    npminstall 完成后 postinstall 的工作位置 并像这样创建一个 server.js 文件:

    const express = require('express');
    const app = express();
    
    app.use(express.static(__dirname + '/dist'));
    
    app.all('*', (req, res) => {
      res.status(200).sendFile(__dirname + '/dist/index.html');
    });
    
    app.listen(process.env.PORT || 8080);
    

    在 dist/index.html 给出 index.html 在 react 中创建的位置

    我不知道反应

    【讨论】:

    • 如果您不明白,请不要犹豫,联系我
    • 谢谢!想要仔细检查,需要构建 server.js 文件并将其添加到构建文件夹中吗?
    • 该脚本是否会与代码中的以下脚本发生冲突?
    猜你喜欢
    • 2021-01-20
    • 1970-01-01
    • 2020-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-16
    • 2021-11-16
    • 2018-09-01
    相关资源
    最近更新 更多