[英]Failed build on Heroku with custom heroku-postbuild script
[英]Mern Stack Application Deployment on Heroku (remote: npm ERR! Failed at the server@1.0.0 heroku-postbuild script.)
我正在尝试在 Heroku 上部署 MERN 堆栈应用程序,但在部署时遇到了一些问题,如下所示
npm ERR! code ENOENT
remote: npm ERR! syscall open
remote: npm ERR! path /tmp/build_9fd3be63/client/package.json
remote: npm ERR! errno -2
remote: npm ERR! enoent ENOENT: no such file or directory, open '/tmp/build_9fd3be63/client/package.json'
remote: npm ERR! enoent This is related to npm not being able to find a file.
remote: npm ERR! enoent
remote:
remote: npm ERR! A complete log of this run can be found in:
remote: npm ERR! /tmp/npmcache.mlfBJ/_logs/2021-05-03T03_26_07_032Z-debug.log
remote: npm ERR! code ELIFECYCLE
remote: npm ERR! errno 254
remote: npm ERR! server@1.0.0 heroku-postbuild: `NPM_CONFIG_PRODUCTION=false npm install --prefix client && npm run build --prefix client`
remote: npm ERR! Exit status 254
remote: npm ERR!
remote: npm ERR! Failed at the server@1.0.0 heroku-postbuild script.
remote: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
remote:
remote: npm ERR! A complete log of this run can be found in:
remote: npm ERR! /tmp/npmcache.mlfBJ/_logs/2021-05-03T03_26_07_059Z-debug.log
服务器的Package.json
{
"name": "server",
"version": "1.0.0",
"description": "",
"engines": {
"node": "14.x"
},
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node index.js",
"heroku-postbuild": "NPM_CONFIG_PRODUCTION=false npm install --prefix client && npm run build --prefix client"
},
"author": "Umar24129",
"license": "ISC",
"dependencies": {
"cors": "^2.8.5",
"express": "^4.17.1",
"mongodb": "^3.6.6"
}
}
客户端的Package.json
{
"name": "react-task-tracker",
"version": "0.1.0",
"private": true,
"dependencies": {
"@testing-library/jest-dom": "^5.12.0",
"@testing-library/react": "^11.2.6",
"@testing-library/user-event": "^12.8.3",
"json-server": "^0.16.3",
"react": "^17.0.2",
"react-datetime-picker": "^3.2.1",
"react-dom": "^17.0.2",
"react-icons": "^4.2.0",
"react-scripts": "4.0.3",
"web-vitals": "^1.1.1"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
文件系统(文件夹层次结构) 截图如下。
我对此进行了很多研究,至少在 StackOverflow 或 Github 上找不到合适的解决方案。 请帮我解决这个问题。 提前致谢。
问题是 npm 无法找到您客户端的 package.json。 “heroku-postbuild”脚本失败,因为它必须安装和构建客户端。 如果您的客户端位于另一个文件夹中,我建议将 --prefix 标记更改为如下所示:
{
"name": "server",
"version": "1.0.0",
"description": "",
"engines": {
"node": "14.x"
},
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node index.js",
"heroku-postbuild": "NPM_CONFIG_PRODUCTION=false cd .. && cd client_dir && npm install && npm run build"
},
"author": "Umar24129",
"license": "ISC",
"dependencies": {
"cors": "^2.8.5",
"express": "^4.17.1",
"mongodb": "^3.6.6"
}
}
So basically you cd out of the server directory (assuming your server package.json is in a server directory in the root) cd into the client directory and then run npm install and build, asumming your client package.json is a folder named client_dir
in根目录。
请在您的本地机器上运行 postbuild 脚本,看看它是否运行正常,如果没有,请发布日志文件,它将在命令行中指定位置。
我的问题是 Node 和 NPM 版本不正确。 在教程中,我在看,NODE 有版本 8.something 和 npm 5.something。 我将其更改为本地计算机上的版本:
"engines": {
"node": "10.19.0",
"npm": "6.14.0"
},
并且应用程序已成功部署。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.