MongoDB and Mongoose - Install and Set Up Mongoose - "Error: Cannot find module"

Hi there,

I am trying to solve the Install and Set Up Mongoose challenge from MongoDB and Mongoose chapter of the Back End Development and APIs course.
To create my DB cluster I followed this tutorial here :

I have created the MONGO_URI env variable using Replit SECRETS tab.
I have added these line in the myApp.js file in order to connect the DB :

const mongoose = require('mongoose');

Finally in my package.json file I have the following dependencies :

  "dependencies": {
    "body-parser": "^1.15.2",
    "dotenv": "^8.2.0",
    "express": "^4.12.4",
    "mongodb": "^3.3.2",
    "mongoose": "^5.11.15"

When I run the code, I get the following error :

Error: Cannot find module ‘/home/runner/boilerplate-mongomongoose/index.js’
Require stack:

  • /nix/store/29fdcv1yhlll61ml8a31qdkrbprxd7gs-prybar-nodejs-0.0.0-67f7a00/prybar_assets/nodejs/module-context-hook.js
  • /tmp/prybar-nodejs-748972092.js
    at Module._resolveFilename (node:internal/modules/cjs/loader:995:15)
    at Function.resolve (node:internal/modules/cjs/helpers:109:19)

Do you have any idea how to fix this ? (I already tried to open the .replit file as it is suggested in some other topics about the “Error: Cannot find module”)

Thanks in advance !

Hello there,

Unfortunately, this error just pops up every now and again. Replit’s config defaults to looking for an index.js file as the entrypoint of an application.

You should be able to edit the .replit file to change the run command to point to server.js instead. The .replit file is automatically created when the REPL is created, and is hidden until you click the Show hidden files option.

As a last resort, you can create a new REPL, and hope the config is correctly created.

Hope this clarifies

1 Like

Thanks a lot ! I changed the first line of the .replit file from :

entrypoint = "index.js"
entrypoint = "server.js"

and it worked :slight_smile: