使用Webpack将NodeJS部署到Heroku:构建失败,错误代码:1

时间:2018-11-12 05:54:39

标签: node.js reactjs heroku webpack

我很难在Heroku上部署React / webpack应用程序,该应用程序在本地可以正常工作。当我尝试部署它时,会得到一个很大的文本字符串,其结尾为“构建失败,错误代码:1”,然后显示“错误:无法将某些引用推送到'https://git.heroku.com/vast-wildwood-63128.git'”。我不太确定问题出在哪里,所以我已经附加了server.js和package.json,如果我缺少任何有用的信息,请告诉我。不知道它是否相关,但是通常我使用Linux,但是最近我一直在Windows上工作。谢谢

编辑:根据建议,使用引擎字段更新了包json

package.json(已通过引擎字段更新)

{
  "name": "enerjee-website",
  "version": "1.0.0",
  "engines": {
    "node": "8.11.2",
    "yarn": "1.7.0",
    "npm": "5.6.0"
  },
  "main": "index.js",
  "author": "Sonam",
  "license": "",
  "scripts": {
    "serve": "live-server public/",
    "build:dev": "webpack",
    "build:prod": "webpack -p --env production",
    "dev-server": "webpack-dev-server",
    "start": "node server/server.js",
    "heroku-postbuild": "yarn run build:prod"
  },
  "dependencies": {
    "@fortawesome/fontawesome-free": "^5.4.2",
    "@fortawesome/fontawesome-svg-core": "^1.2.8",
    "@fortawesome/free-brands-svg-icons": "^5.5.0",
    "@fortawesome/free-solid-svg-icons": "^5.5.0",
    "@fortawesome/react-fontawesome": "^0.1.3",
    "babel-cli": "6.24.1",
    "babel-core": "6.25.0",
    "babel-loader": "7.1.1",
    "babel-plugin-transform-class-properties": "6.24.1",
    "babel-preset-env": "1.5.2",
    "babel-preset-react": "6.24.1",
    "bootstrap": "^4.1.3",
    "css-loader": "0.28.4",
    "express": "^4.16.3",
    "jquery": "^3.3.1",
    "live-server": "^1.2.0",
    "node-sass": "4.5.3",
    "normalize.css": "7.0.0",
    "popper.js": "^1.14.4",
    "react": "15.6.1",
    "react-dom": "15.6.1",
    "react-modal": "2.2.2",
    "react-router-dom": "4.1.2",
    "sass-loader": "6.0.6",
    "style-loader": "0.18.2",
    "validator": "8.0.0",
    "webpack": "3.1.0",
    "webpack-dev-server": "2.5.1"
  },
  "devDependencies": {
    "file-loader": "^2.0.0",
    "image-webpack-loader": "^4.3.1"
  }
}

server.js

const path = require('path');
const express = require('express');
const app = express();
const publicPath = path.join(__dirname, '..', 'public');
const port = process.env.PORT || 3000;

app.use(express.static(publicPath));

app.get('*', (req, res) => {
  res.sendFile(path.join(publicPath, 'index.html'));
});

app.listen(port, () => {
  console.log('Server is up on' + port);
});

添加引擎后出现错误文本

remote: -----> Node.js app detected
remote:
remote: -----> Creating runtime environment
remote:
remote:        NPM_CONFIG_LOGLEVEL=error
remote:        NODE_ENV=production
remote:        NODE_MODULES_CACHE=true
remote:        NODE_VERBOSE=false
remote:
remote: -----> Installing binaries
remote:        engines.node (package.json):  8.x|10.x
remote:        engines.npm (package.json):   unspecified (use default)
remote:        engines.yarn (package.json):  unspecified (use default)
remote:
remote:        Resolving node version 8.x|10.x...
remote:        Error: Invalid semantic version "8.x|10.x"
remote:
remote: -----> Build failed
remote:
remote:  !     Invalid semver requirement
remote:
remote:        Node, Yarn, and npm adhere to semver, the semantic versioning convention
remote:        popularized by GitHub.
remote:
remote:        http://semver.org/
remote:
remote:        However you have specified a version requirement that is not a valid
remote:        semantic version.
remote:
remote:        https://kb.heroku.com/why-is-my-node-js-build-failing-because-of-an-invalid-semver-requirement
remote:
remote:  !     Push rejected, failed to compile Node.js app.
remote:
remote:  !     Push failed
remote: Verifying deploy...
remote:
remote: !       Push rejected to vast-wildwood-63128.
remote:
To https://git.heroku.com/vast-wildwood-63128.git
 ! [remote rejected] master -> master (pre-receive hook declined)
error: failed to push some refs to 'https://git.heroku.com/vast-wildwood-63128.git'

1 个答案:

答案 0 :(得分:0)

您需要在package.json中指定节点版本,以便Heroku知道要运行哪个节点版本。

例如

"engines": {
  "node": "8.x|10.x"
},

https://devcenter.heroku.com/articles/nodejs-support

阅读指定的node.js版本部分。