Search code examples
node.jsmongodbmongoose

Node server crashes as soon as mongoose is imported in the entry file


Facing this error as soon as i import mongoose in my entry point server.js file

/home/kashif/git/edemy/server/node_modules/mongodb/lib/collection.js:74
            pkFactory: db.options?.pkFactory ?? utils_1.DEFAULT_PK_FACTORY,
                                  ^

SyntaxError: Invalid or unexpected token
    at Module._extensions..js (node:internal/modules/cjs/loader:1309:10)

Node.js v20.2.0

Server crashes with a syntax error in node module file as soon as i import mongoose in server.js file Server crashed after mongoose imported

Before mongoose import server is running fine Server running fine before mongoose impoer

Server.js file:

// setting server using express now
import express from "express";
import cors from "cors";

// import fs from "fs"; we dont need the whole file syst instead we can destructure just the readdirSync function

import { readdirSync } from "fs";
import morgan from "morgan";
import mongoose from "mongoose";

// import doenv config

import dotenv from "dotenv";
dotenv.config();

// create express app

const app = express(); // when we execute exrpess that entire express app is available in this variable app

// apply middlewares ( code which runs between request and response)

app.use(cors()); // cors is a middleware which allows to connect to the server from different domain
app.use(express.json()); // express.json() is a middleware which allows to read json data from request body
app.use(morgan("dev")); // morgan is a middleware which logs the request details in the console

//Routes

readdirSync("./routes").map((r) => {
  //apply as middelware now
  app.use("/api", require(`./routes/${r}`));
});

const port = process.env.PORT || 8000; // process.env.PORT is used to get the port number from the .env file and if it is not available then it will use 8000 as default port. Process is a global variable in nodejs which allow us to access the environment variables and other process related information

// start listening to port

app.listen(port, () => {
  console.log(`server is running on port ${port}`);
});

Package.json file:

{
  "name": "server",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "nodemon -r esm server.js"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "bcrypt": "^5.1.1",
    "cors": "^2.8.5",
    "dotenv": "^16.3.1",
    "esm": "^3.2.25",
    "express": "^4.18.2",
    "jsonwebtoken": "^9.0.1",
    "mongoose": "^7.5.0",
    "morgan": "^1.10.0",
    "nodemon": "^3.0.1"
  }
}

I am trying to use mongoose to connect to my mongoDB database, but as soon as I import mongoose in my entry point server.js file, it gives a syntax error in node modules file stated above


Solution

  • This is a known issue with mongoose v7.

    Downgrade your mongoose version to latest minor of v6. The issue should be resolved.

    $ npm uninstall mongoose
    $ npm install mongoose@6