如何修复Node JS错误在Heroku上找不到模块'express'?

时间:2020-08-19 23:08:14

标签: javascript node.js express heroku heroku-cli

我有下面的节点js代码,它在本地工作得很好,但是当我将其部署到heroku时,出现错误消息“错误:找不到模块'express' 内部/模块/cjs/loader.js:968抛出错误”

    const port = process.env.PORT || 5000;
    const express = require('express');
    require('dotenv').config();
    const app = express();
   
    app.set('view-engine','ejs');
    app.use(express.urlencoded({extended:false}));
    app.get('/',(req,res) => {
    res.render('index.ejs');
   });
   app.listen(port, ()=>{console.log(`Listening on http://localhost:${port}`)});

package.json

{
  "name": "mtNodeApp",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "node index.js",
    "devStart": "nodemon index.js"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "bcrypt": "^5.0.0",
    "body-parser": "^1.19.0",
    "dotenv": "^8.2.0",
    "ejs": "^3.1.3",
    "express": "^4.17.1",
    "mongoose": "^5.10.0",
    "nodemon": "^2.0.4",
    "request": "^2.88.2"
  }
}

任何想法,我不确定我缺少什么吗?!

谢谢

1 个答案:

答案 0 :(得分:2)

Express必须位于dependencies而非devDependecies上:

{
 ...
  "dependencies" {
    "express": "^4.17.1",
    ...
  }
  "devDependecies": {
  ...
}

此外,可能大多数都必须位于dependencies中,以便服务器知道必须安装的内容。基本上,它会忽略模块的安装,因为这些模块是用于开发而非生产的。

编辑:如果您想了解更多信息,可以阅读这个好答案。

Stackoverflow answer