Node/React 应用无法部署到 Heroku

4
我的应用程序有一个React前端和一个Node后端;BE通过API为FE提供数据。它无法部署到Heroku,下面是堆栈跟踪。
我尝试过的事情:
1. 这个问题。我尝试了模仿答案中提到的模型存储库的package.json文件,但没有成功。 2. 将上述模型存储库部署到自己的失败Heroku应用程序 - 它正常工作,因此Heroku不是问题。 3. 这个问题(我已检查react-scripts被列为依赖项而不是dev-dependency)。
我最终可能会以模型存储库作为起点,并逐步复制我的项目,但非常感谢任何关于可能出错的信息。
堆栈跟踪:
> fsevents@1.0.17 install /tmp/build_f03b382a2b768aeb89ff9c197145d0a5/client/node_modules/fsevents
> node-pre-gyp install --fallback-to-build

node-pre-gyp ERR! Tried to download(404): https://fsevents-binaries.s3-us-west-2.amazonaws.com/v1.0.17/fse-v1.0.17-node-v48-linux-x64.tar.gz
node-pre-gyp ERR! Pre-built binaries not found for fsevents@1.0.17 and node@6.10.3 (node-v48 ABI) (falling back to source compile with node-gyp)
make: Entering directory '/tmp/build_f03b382a2b768aeb89ff9c197145d0a5/client/node_modules/fsevents/build'
SOLINK_MODULE(target) Release/obj.target/.node
COPY Release/.node
make: Leaving directory '/tmp/build_f03b382a2b768aeb89ff9c197145d0a5/client/node_modules/fsevents/build'

> fibers@1.0.15 install /tmp/build_f03b382a2b768aeb89ff9c197145d0a5/client/node_modules/fibers
> node build.js || nodejs build.js

`linux-x64-48` exists; testing
Binary is fine; exiting
added 1242 packages in 52.433s
removed 1242 packages in 25.561s

> react-ui@0.1.0 build /tmp/build_f03b382a2b768aeb89ff9c197145d0a5/client
> react-scripts build

sh: 1: react-scripts: not found
npm ERR! file sh
npm ERR! code ELIFECYCLE
npm ERR! errno ENOENT
npm ERR! syscall spawn
npm ERR! react-ui@0.1.0 build: `react-scripts build`
npm ERR! spawn ENOENT
npm ERR!
npm ERR! Failed at the react-ui@0.1.0 build script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

我的服务器 package.json 文件:

{
  "name": "in-search-of-happiness",
  "engines": {
    "node": "6.10.x"
  },
  "version": "0.0.0",
  "private": true,
  "scripts": {
    "start": "node ./bin/www",
    "heroku-postbuild": "cd client/ && npm install --only=dev && npm install && npm run build"
  },
  "cacheDirectories": [
    "node_modules",
    "client/node_modules"
  ],
  "dependencies": {
    "body-parser": "^1.17.2",
    "cookie-parser": "^1.4.3",
    "debug": "~2.6.3",
    "express": "~4.15.2",
    "jade": "^1.11.0",
    "mongoose": "^4.10.8",
    "morgan": "^1.8.2",
    "serve-favicon": "^2.4.3",
    "zombie": "^5.0.5"
  }
}

我的React package.json文件:

{
  "name": "react-ui",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "bootstrap": "^3.3.7",
    "fetch": "^1.1.0",
    "node-fetch": "^1.7.1",
    "process-nextick-args": "^1.0.7",
    "react": "^15.6.1",
    "react-bootstrap": "^0.31.0",
    "react-dom": "^15.6.1",
    "react-scripts": "1.0.7",
    "util-deprecate": "^1.0.2"
  },
  "devDependencies": {
    "chai": "^4.0.2",
    "eslint": "^4.0.0",
    "sinon": "^2.3.5",
    "wdio-mocha-framework": "^0.5.10",
    "webdriverio": "^4.8.0"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test --env=jsdom",
    "eject": "react-scripts eject"
  },
  "proxy": "http://localhost:3001"
}

你最终想出了解决方案吗? - Vice Sallés
1
嗨@ViceSallés,如果我没记错的话,一个同事的节点配置已经损坏了package-lock.json文件。正如我在问题中提到的那样,我们最终将代码复制到模型存储库中。 - achalk
1个回答

0

你好,你的发布构建脚本需要进行一些修改,请按照以下约定进行操作:

将 reactFolderName 替换为包含 React 前端的文件夹名称 在 package.json 文件的 scripts 部分添加以下代码片段

    scripts{
    "heroku-postbuild": "NPM_CONFIG_PRODUCTION=false npm install --prefix reactFolderName && npm run build --prefix reactFolderName"
}

并将此片段添加到您的index.js文件中

    app = express()
    app.use(express.static('reactFolderName/build'));
    app.get('*', (req, res) => res.sendFile(path.resolve(__dirname, 'reactFolderName', 'build', 'index.html')));

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接