在heroku 中部署mern 应用程序时出现npm 错误并且构建失败

时间:2021-06-25 11:36:03

标签: heroku

嗨,我是 Heroku 的新手,在部署我的 mern 应用程序时遇到错误。请帮忙 我试过添加这样的“引擎”:{ “节点”:“16.2.1” 但现在仍在工作,我不知道我做对了什么,因为我是 react 和 heroku 的新手。 我还检查了一些教程和文档,但仍然无法弄清楚如何解决这个问题。 我将不胜感激这方面的任何帮助。 我有后端和前端文件夹,我的 server.js 在后端文件夹中

-----> Building on the Heroku-20 stack
 -----> Determining which buildpack to use for this app
-----> Node.js app detected
   
-----> Creating runtime environment
   
   NPM_CONFIG_LOGLEVEL=error
   NODE_VERBOSE=false
   NODE_ENV=production
   NODE_MODULES_CACHE=true
   
-----> Installing binaries
   engines.node (package.json):  unspecified
   engines.npm (package.json):   unspecified (use default)
   
   Resolving node version 14.x...
   Downloading and installing node 14.17.1...
   Using default npm version: 6.14.13
   
-----> Installing dependencies
   Installing node modules
   
   > nodemon@2.0.7 postinstall /tmp/build_cade1f5a/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 311 packages in 7.986s
   
-----> Build
   Running heroku-postbuild
   
   > mern-shop@1.0.0 heroku-postbuild /tmp/build_cade1f5a
   > cd frontend && npm install && npm run build
   
npm ERR! code EBADPLATFORM
npm ERR! notsup Unsupported platform for fsevents@2.3.2: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})
 npm ERR! notsup Valid OS:    darwin
 npm ERR! notsup Valid Arch:  any
 npm ERR! notsup Actual OS:   linux
 npm ERR! notsup Actual Arch: x64
 npm ERR! A complete log of this run can be found in:
 npm ERR!     /tmp/npmcache.KC26k/_logs/2021-06-25T11_19_06_297Z-debug.log
 npm ERR! code ELIFECYCLE
 npm ERR! errno 1
 npm ERR! mern-shop@1.0.0 heroku-postbuild: `cd frontend && npm install && npm run build`
 npm ERR! Exit status 1
 npm ERR! 
npm ERR! Failed at the mern-shop@1.0.0 heroku-postbuild script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output 
above.
npm ERR! A complete log of this run can be found in:
npm ERR!     /tmp/npmcache.KC26k/_logs/2021-06-25T11_19_06_348Z-debug.log
-----> Build failed
   
   We're sorry this build is failing! You can troubleshoot common issues here:
   https://devcenter.heroku.com/articles/troubleshooting-node-deploys
   
   Some possible problems:
   
   - Node version not specified in package.json
     https://devcenter.heroku.com/articles/nodejs-support#specifying-a-node-js-version
   
   Love,
   Heroku
   
 !     Push rejected, failed to compile Node.js app.
 !     Push failed

下面是我的 package.json

{
  "name": "mern-shop",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "node backend/server.js",
    "server": "nodemon backend/server.js",
    "client": "npm start --prefix frontend",
    "dev": "concurrently \"npm run server\" \"npm run client\"",
    "data:import": "node backend/seederScript",
    "heroku-postbuild": "cd frontend && npm install && npm run build"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "bootstrap": "^4.3.1",
    "dotenv": "^10.0.0",
    "express": "^4.17.1",
    "mongoose": "^5.12.13",
    "react-bootstrap": "^1.6.1",
    "styled-components": "^5.3.0"
  },
  "devDependencies": {
    "concurrently": "^6.2.0",
    "nodemon": "^2.0.7"
  }
}

<!-- end snippet -->


<!-- end snippet -->

下面是我的后端/server.js

require("dotenv").config();
const express = require("express");
const productRoutes = require("./routes/productRoutes");
const connectDB = require("./config/db");
const path = require('path');

connectDB();

const app = express();

app.use(express.json());

app.get("/", (req, res) => {
  res.json({ message: "API running..." });
});


app.use("/api/products", productRoutes);

//Serve static assets if in production
if(process.env.NODE_ENV === 'production') {
  //Set static folder
  app.use(express.static('frontend/build'));

  app.get('*', (req, res) => {
    res.sendFile(path.join(__dirname, 'frontend', 'build', 'index.html'));
  });
}

const PORT = process.env.PORT || 6000;
app.listen(PORT, () => console.log(`Server running on port ${PORT}`));

1 个答案:

答案 0 :(得分:1)

首先检查和 npm 版本

$ node -v >> v16.2.0

$ npm -v >> 7.13.0

然后在引擎中检查您的服务器 package.json 更新相同的版本。

如果一切正常试试这个:npm install fsevents --save,你似乎没有安装这个