我曾经尝试在heroku上部署MERN堆栈应用程序。我已经按照heroku文档中给出的过程进行了操作。但是,不知何故,我无法将该应用程序部署到heroku上。
下面是我的server.js代码:
const express = require('express');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const path = require('path');
const items = require('./routes/api/items');
const app = express();
app.use(bodyParser.json());
const db = require('./config/keys').mongoURI;
mongoose
.connect(db)
.then(() => console.log('MongoDB Connected...'))
.catch(err => console.log(err));
app.use('/api/items', items);
if (process.env.NODE_ENV === 'production') {
app.use(express.static('client/build'));
app.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, 'client', 'build', 'index.html'));
});
}
const port = process.env.PORT || 5000;
app.listen(port, () => console.log(`Server started on port ${port}`));
下面是我在服务器文件夹中的package.json文件:
{
"name": "mern_shopping_list",
"version": "1.0.0",
"description": "",
"main": "server.js",
"scripts": {
"client-install" : "npm install --prefix client",
"start": "node server.js",
"server": "nodemon server.js",
"client": "npm 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.1.0",
"express": "^4.17.1",
"mongoose": "^5.9.0"
},
"devDependencies": {
"nodemon": "^2.0.2"
}
}
我得到的错误如下:
remote: -----> Build
remote: Running heroku-postbuild
remote:
remote: > mern_shopping_list@1.0.0 heroku-postbuild
/tmp/build_6dafb80a10b85bd553ee344141227145
remote: > NPM_CONFIG_PRODUCTION=false npm install --prefix client && npm run build --prefix
client
remote:
remote: up to date in 0.284s
remote: found 0 vulnerabilities
remote:
remote: npm ERR! code ENOENT
remote: npm ERR! syscall open
remote: npm ERR! path /tmp/build_6dafb80a10b85bd553ee344141227145/client/package.json
remote: npm ERR! errno -2
remote: npm ERR! enoent ENOENT: no such file or directory, open
'/tmp/build_6dafb80a10b85bd553ee344141227145/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.2Rtdv/_logs/2020-02-15T15_35_57_654Z-debug.log
remote: npm ERR! code ELIFECYCLE
remote: npm ERR! errno 254
remote: npm ERR! mern_shopping_list@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 mern_shopping_list@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.2Rtdv/_logs/2020-02-15T15_35_57_666Z-debug.log
remote:
remote: -----> Build failed
remote:
remote: We're sorry this build is failing! You can troubleshoot common issues here:
remote: https://devcenter.heroku.com/articles/troubleshooting-node-deploys
remote:
remote: Some possible problems:
remote:
remote: - node_modules checked into source control
remote: https://blog.heroku.com/node-habits-2016#9-only-git-the-important-bits
remote:
remote: - Node version not specified in package.json
remote: https://devcenter.heroku.com/articles/nodejs-support#specifying-a-node-js-version
remote:
remote: Love,
remote: Heroku
remote:
remote: ! Push rejected, failed to compile Node.js app.
remote:
remote: ! Push failed
remote: Verifying deploy...
remote:
remote: ! Push rejected to damp-anchorage-46764.
remote:
To https://git.heroku.com/damp-anchorage-46764.git
! [remote rejected] master -> master (pre-receive hook declined)
error: failed to push some refs to 'https://git.heroku.com/damp-anchorage-46764.git'
我已经尝试了一些在线链接,但没有一个是给出所需的结果。谁能建议我如何解决这个问题?
5条答案
按热度按时间gmxoilav1#
cd到您的/client remove .git文件夹
也可以通过以下命令重置git:
正如Mina Amir所提到的,您的更改已经被客户端目录中的git跟踪,并且不会再被提交中的server/main git跟踪
qkf9rpyu2#
我遇到了这个问题,当我复制我的项目,并尝试部署,复制您的后端和客户端没有node_modules,然后尝试npm安装在您的后端和客户端文件夹中,然后尝试运行您的应用程序,在这一点上,我得到了一个错误的东西称为“babel-jest”依赖关系,所以我安装了它,并编辑它的版本为这个“^24.8.0”手动在package.json中,我再次在根文件夹(后端)中运行npm安装,然后我云恢复错误并运行应用程序,
之后,我开始部署过程中,我遵循以下步骤,它的工作。
https://www.youtube.com/watch?v=qdoiwouykAg&list=PL8E3OP1AP-EneRlZoCIQ996os2DLegipk&index=3&t=873s
wgmfuz8q3#
尝试从/client中删除.git文件夹,因为根目录中的main .git文件夹看不到/client/package. json,并且不跟踪/client中的任何更改。之后,从根目录中的开始提交所有内容
6ie5vjzr4#
如果你仍然有这个问题,,我遇到了一个类似的问题,什么解决了我的问题是简单地重命名我的客户端文件夹为“客户端”,以匹配该路径上指定的名称。希望有人发现这有帮助,并节省时间
jucafojl5#
我也遇到过这种情况。以下是确切的解决方案:-
问题是由两个.git文件夹引起的。一个.git文件夹在root目录中,另一个在**“client”**目录中。
要解决此问题,请删除两个.git文件夹,并在删除两个.git文件夹后使用git init在根目录中重新初始化git。
(Note ReactJS自动创建了它自己的.git文件夹,这导致了这个问题,我们必须不惜一切代价删除它。此外,如果你是在布拉德Traversey的教程的后续,他也跳过了这个重要部分。这是他的一个错误)