简体   繁体   中英

How to fix Node JS error cannot find module 'express' on heroku?

I have the below node.js code that it works perfectly fine locally but when I deploy it to heroku I get an error says "Error: Cannot find module 'express' internal/modules/cjs/loader.js:968 throw error"

    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"
  }
}

Any thoughts, I am not sure what I am missing?!

Thank you

Express must be on dependencies instead of devDependecies :

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

Also, probably most of them must be in dependencies , in order for the server to know what must be installed. Basically, it's ignore the installation of your modules since those are for development and not production.

Edit: If you wanna know more about it, you can read this great answer.

Stackoverflow answer

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM