在Heroku上部署时遇到麻烦

时间:2018-11-07 17:41:15

标签: node.js reactjs heroku deployment mern

我正在尝试部署我在Heroku上创建的项目,这样做有些麻烦...

我不知道我的部署有什么问题,希望大家能为我提供帮助:)

我认为这是一个不确定的问题...

remote: Compressing source files... done.
remote: Building source:
remote:
remote: -----> Node.js app detected
remote:
remote: -----> Creating runtime environment
remote:
remote:        NPM_CONFIG_LOGLEVEL=error
remote:        NODE_ENV=production
remote:        NODE_MODULES_CACHE=true
remote:        NODE_VERBOSE=false
remote:
remote: -----> Installing binaries
remote:        engines.node (package.json):  10.11.0
remote:        engines.npm (package.json):   unspecified (use default)
remote:
remote:        Resolving node version 10.11.0...
remote:        Downloading and installing node 10.11.0...
remote:        Using default npm version: 6.4.1
remote:
remote: -----> Building dependencies
remote:        Installing node modules (package.json + package-lock)
remote:
remote:        > nodemon@1.18.4 postinstall /tmp/build_9767eb39664bb3efb05f0e963d14a2f1/node_modules/nodemon
remote:        > node bin/postinstall || exit 0
remote:
remote:        Love nodemon? You can now support the project via the open collective:
remote:         > https://opencollective.com/nodemon/donate
remote:
remote:        added 319 packages from 191 contributors and audited 2482 packages in 7.784s
remote:        found 0 vulnerabilities
remote:
remote:        Running heroku-postbuild
remote:
remote:        > server@1.0.0 heroku-postbuild /tmp/build_9767eb39664bb3efb05f0e963d14a2f1
remote:        > NPM_CONFIG_PRODUCTION=false npm install --prefix client && npm run build --prefix client
remote:
remote:        up to date in 0.76s
remote:        found 0 vulnerabilities
remote:
remote: npm ERR! path /tmp/build_9767eb39664bb3efb05f0e963d14a2f1/client/package.json
remote: npm ERR! code ENOENT
remote: npm ERR! errno -2
remote: npm ERR! syscall open
remote: npm ERR! enoent ENOENT: no such file or directory, open '/tmp/build_9767eb39664bb3efb05f0e963d14a2f1/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.vWMnG/_logs/2018-11-07T16_38_24_492Z-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.vWMnG/_logs/2018-11-07T16_38_24_505Z-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:        If you're stuck, please submit a ticket so we can help:
remote:        https://help.heroku.com/
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 agile-badlands-25978.
remote:
To https://git.heroku.com/agile-badlands-25978.git
 ! [remote rejected] master -> master (pre-receive hook declined)

我的项目:

PROJETNAME
- client
    --- node_modules
    --- public
    --- src
    --- package.lock.json
    --- package.json
    --- .gitignore
- node_modules
- config
- models
- routes
- validation
- .gitignore
- package.lock.json
- package.json
- server.js

server.js

const express = require('express');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const passport = require('passport');
const path = require('path');

// Routes
const users = require('./routes/api/users');
const shops = require('./routes/api/shops');
const profile = require('./routes/api/profile');
const mail = require('./routes/contact/mail');

const app = express();

// Body parser middleware
app.use(bodyParser.urlencoded({extended: false}));
app.use(bodyParser.json());

// DB Config
const db = require('./config/keys').mongoURI;

// Connect to MongoDB
mongoose
    .connect(db)
    .then(() => console.log('MongoDB Connected'))
    .catch(err => console.log(err))


// Passport Middleware
app.use(passport.initialize());

// Passport Config
require('./config/passport')(passport);

// Use Routes
app.use('/api/users', users);
app.use('/api/shops', shops);
app.use('/api/profile', profile);
app.use('/contact/mail', mail);

// Server static assets if in production
if (process.env.NODE_ENV === 'production') {
    // Set static folder
    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 running on ${port}`));

server => package.json

{
  "name": "server",
  "version": "1.0.0",
  "engines": {
    "node": "10.11.0"
  },
  "description": "",
  "main": "server.js",
  "scripts": {
    "start": "node server.js",
    "dev": "nodemon server.js",
    "heroku-postbuild": "NPM_CONFIG_PRODUCTION=false npm install --prefix client && npm run build --prefix client"
  },
  "dependencies": {
    "bcryptjs": "^2.4.3",
    "body-parser": "^1.18.3",
    "express": "^4.16.4",
    "jsonwebtoken": "^8.3.0",
    "mongoose": "^5.3.5",
    "nodemailer": "^4.6.8",
    "nodemon": "^1.18.4",
    "passport": "^0.4.0",
    "passport-jwt": "^4.0.0",
    "validator": "^10.8.0"
  },
  "devDependencies": {},
  "author": "Cyril G",
  "license": "ISC"
}

1 个答案:

答案 0 :(得分:1)

此问题实际上是由于客户端目录中的.git文件夹引起的。 从客户端删除该.git目录并运行

git rm -f --cached client && git add . && git commit -m 'Add client folder back to git repo' && git push heroku master

这里解决了: https://github.com/bradtraversy/mern_shopping_list/issues/7