ayoub
ayoub

Reputation: 93

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

Upvotes: 0

Views: 3974

Answers (1)

kachus22
kachus22

Reputation: 439

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

Upvotes: 4

Related Questions