我正试图在Heroku上对一个udemy课程的React项目进行生产构建。 我尝试了很多东西来使它工作,但我完全不知道。
从Heroku构建过程的输出。
-------------------------------------------------------------------------------------------------------------->创建运行时环境
NPM_CONFIG_LOGLEVEL=error NODE_ENV=production NODE_MODULES_CACHE=true NODE_VERBOSE=false
----> 安装二进制文件 engines.node (package.json): 12.17.0 engines.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
---------------------------------------------------------------------------------------------------------------> 安装依赖关系 安装节点模块(package.json)
> [email protected] 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
> [email protected] 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
npm ERR! errno -2 npm ERR! enoent ENOENT: no such file or directory, open 'tmpbuild_01409f5f7cae8788b3a2ccca28c521d8clientpackage.json' npm ERR! enoent 这与npm找不到文件有关。这次运行的完整日志可以在下面找到: npm ERR! tmpnpmcache.h4E00_logs2020-05-27T18_18_35_286Z-debug.log npm ERR! code ELIFECYCLE npm ERR! errno 254 npm ERR! [email protected] heroku-postbuild:
NPM_CONFIG_PRODUCTION=false npm install --prefix client && npm run build --prefix client
npm ERR! 退出状态 254 npm ERR! 在 [email protected] heroku-postbuild 脚本中失败。这可能不是npm的问题。npm ERR! 这次运行的完整日志可以在以下文件中找到:npm ERR!tmpnpmcache.h4E00_logs2020-05-27T18_18_35_303Z-debug.log -----> Build failedWe'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"));
});
}
服务器上的package. 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
原来由于我没有删除客户端目录下的create-react-app创建的.git,这就停止了服务器仓库包括前端文件的运行。 所以只得删除这个,重新初始化服务器git仓库。