【发布时间】:2020-09-14 22:20:29
【问题描述】:
我正在尝试在 udemy 课程 React 项目的 Heroku 上进行生产构建。我已经尝试了很多方法来让它发挥作用,但我完全不知所措。
Heroku 构建过程的输出:
-----> 检测到 Node.js 应用程序
-----> 创建运行环境
NPM_CONFIG_LOGLEVEL=error NODE_ENV=production NODE_MODULES_CACHE=true NODE_VERBOSE=false-----> 安装二进制文件 引擎.node(package.json):12.17.0 engine.npm (package.json): 6.14.5
Resolving node version 12.17.0... Downloading and installing node 12.17.0... Bootstrapping npm 6.14.5 (replacing 6.14.4)... npm 6.14.5 installed-----> 恢复缓存 由于 node、npm、yarn 或 stack 的版本发生变化,缓存目录未恢复 此版本的模块安装可能需要更长的时间
-----> 安装依赖 安装节点模块(package.json)
> nodemon@2.0.4 postinstall /tmp/build_01409f5f7cae8788b3a2ccca28c521d8/node_modules/nodemon > node bin/postinstall || exit 0 Love nodemon? You can now support the project via the open collective: > https://opencollective.com/nodemon/donate added 267 packages from 183 contributors and audited 268 packages in 11.725s 12 packages are looking for funding run `npm fund` for details found 0 vulnerabilities-----> 构建 运行 heroku-postbuild
> server@1.0.0 heroku-postbuild /tmp/build_01409f5f7cae8788b3a2ccca28c521d8 > NPM_CONFIG_PRODUCTION=false npm install --prefix client && npm run build --prefix client up to date in 0.618s found 0 vulnerabilities npm ERR! code ENOENT npm ERR! syscall open npm ERR! path /tmp/build_01409f5f7cae8788b3a2ccca28c521d8/client/package.json npm错误! errno -2 npm 错误! enoent ENOENT:没有这样的文件或目录,打开 '/tmp/build_01409f5f7cae8788b3a2ccca28c521d8/client/package.json' npm 呃! enoent 这与 npm 找不到文件有关。 npm 呃! enoent npm 错误!可以在以下位置找到此运行的完整日志:npm 呃! /tmp/npmcache.h4E00/_logs/2020-05-27T18_18_35_286Z-debug.log npm 错误!代码 ELIFECYCLE npm 错误!错误号 254 npm 错误!服务器@1.0.0 heroku-postbuild:
NPM_CONFIG_PRODUCTION=false npm install --prefix client && npm run build --prefix clientnpm ERR!退出状态 254 npm 呃! npm 错误!在 server@1.0.0 heroku-postbuild 脚本中失败。 npm 呃!这可能不是 npm 的问题。有可能 上面的附加日志输出。 npm 错误!本次运行的完整日志 可以在以下位置找到:npm ERR!
/tmp/npmcache.h4E00/_logs/2020-05-27T18_18_35_303Z-debug.log -----> 构建失败We're sorry this build is failing! You can troubleshoot common issues here: https://devcenter.heroku.com/articles/troubleshooting-node-deploys If you're stuck, please submit a ticket so we can help: https://help.heroku.com/ Love, Heroku ! Push rejected, failed to compile Node.js app. ! Push failed
来自服务器 index.js:
if (process.env.NODE_ENV === 'production') {
// Express will serve production assets
app.use(express.static('client/build'));
// Express will serve index.html file if it doesn't recognise the route
const path = require('path');
app.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, 'client', 'build', "index.html"));
});
}
服务器包.json:
{
"name": "server",
"version": "1.0.0",
"description": "",
"main": "index.js",
"engines": {
"node": "12.17.0",
"npm": "6.14.5"
},
"scripts": {
"start": "node index.js",
"server": "nodemon index.js",
"client": "npm run start --prefix client",
"dev": "concurrently \"npm run server\" \"npm run client\"",
"heroku-postbuild": "NPM_CONFIG_PRODUCTION=false npm install --prefix client && npm run build --prefix client"
},
"author": "",
"license": "ISC",
"dependencies": {
"body-parser": "^1.19.0",
"concurrently": "^5.2.0",
"cookie-session": "^1.4.0",
"express": "^4.17.1",
"mongoose": "^5.9.15",
"nodemon": "^2.0.4",
"passport": "^0.4.1",
"passport-google-oauth20": "^2.0.0",
"stripe": "^8.55.0"
}
}
.gitignore:
node_modules
dev.js
【问题讨论】:
标签: javascript node.js reactjs express heroku