How to Check if Mongoose is Installed in Nodejs?

Check If Mongoose Is Installed Featured Image

In this guide, I will explain how to check if Mongoose is installed into your Nodejs project.

With the assistance of operators and functions in MongoDB, you may alter data querying, printing, editing, and more.

In this guide, I want to help you check if Mongoose is installed in your Nodejs application. We will create a small application to explain the topic. So, let us get started.

How to Check if Mongoose in Installed in Nodejs?

  • Open your system terminal and create a project directory, then, move into it:
mkdir mongooseCheck
cd mongooseCheck
  • Create a new Nodejs project along with the entry point file:
npm init -y
touch index.js

You will now see a package.json file created automatically for you.

  • Install the mongoose and express package in your Nodejs project:
npm i mongoose express

after this, you should see node_modules created automatically for you. Let us now proceed to check whether Mongoose has been installed.

Method 1: Check package.json file

  • Open up the package.json
  • Look for the “dependencies” property which is an object, in the file
  • You should now see a “mongoose” property inside dependencies

This is how my package.json looks like:

{
  "name": "shoe-store",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "Aneesha S",
  "license": "ISC",
  "dependencies": {
    "express": "^4.17.1",
    "mongoose": "^5.11.17"
  }
}

Method 2: Check the ‘node_modules’ directory of your project

In your project directory, pull down the node_modules directory. Here, you will see multiple directories with the package name you have installed along with some of the ones you didn’t.

Look for a directory named “mongoose”. This is the directory where all the Mongoose files reside for your project.

Method 3: Connect to MongoDB & Insert Documents

If for some reason you are unable to perform the above steps or if the above methods didn’t work for you, then the last method you can go to check if mongoose is installed, is to connect to MongoDB, create a schema of your data and insert a few documents and check if Mongoose s installed or not.

Let us get going.

  • Create a models directory inside your project directory
  • Now, create a file for your model. I will name mine shoes.js
  • Inside the shoes.js file, import the Mongoose package and create a data schema as shown below:
const mongoose = require('mongoose');

const productSchema = new mongoose.Schema({
    name: {
        type: String,
    },
    price: {
        type: Number,
    },
    size: {
        type: Number
    },
    style: {
        type: String,
    },
    onSale: {
        type: Boolean
    },
    freeDelivery: {
        type: Boolean
    }
})
  • Create the model using the schema so MongoDB can create a ‘shoes’ collection for us. Then, export the created model:
const Shoe = mongoose.model('Shoe', productSchema);
module.exports = Shoe;
  • Open the index.js file and import Mongoose and the created model. Also, connect Mongoose to a local MongoDB database:
const express = require('express');
const app = express();
const mongoose = require('mongoose');
const Shoe = require('./models/product.js');

mongoose.connect('mongodb://localhost:27017/shoeStore', { useNewUrlParser: true, useUnifiedTopology: true })
    .then(() => {
        console.log(`CONNECTED TO MONGO USING MONGOOSE!`);
    })
    .catch((err) => {
        console.log(`OH NO! MONGOOSE MONGO CONNECTION ERROR!`);
        console.log(err);
    })

We have now connected our Nodejs application to a MongoDB database, named ‘shoeStore’.

  • We will now insert a few documents inside our shoeStore database from a different file.
  • Create a seed.js file inside your project directory
  • In this file, we will import Mongoose and the Shoe model again and insert a few documents based on it. We will also have to create a Mongo database connection as we will use this file only to insert documents:
const mongoose = require('mongoose');
const Shoe = require('./models/product.js')

mongoose.connect('mongodb://localhost:27017/shoeStore', { useNewUrlParser: true, useUnifiedTopology: true })
    .then(() => {
        console.log(`CONNECTED TO MONGO!`);
    })
    .catch((err) => {
        console.log(`OH NO! MONGO CONNECTION ERROR!`);
        console.log(err);
    })

const seedProducts = [
    {
        "name": "Mariana Grider Boots",
        "price": 23000,
        "size": 7,
        "style": "Slingbacks",
        "onSale": false,
        "freeDelivery": true
    },
    {
        "name": "Purple Shimmery Heels",
        "price": 13380,
        "size": 6,
        "style": "Peep Toes",
        "onSale": true,
        "freeDelivery": false
    },
    {
        "name": "Zac Porter Straps",
        "price": 19760,
        "size": 8,
        "style": "Ankle Strap",
        "onSale": true,
        "freeDelivery": true
    },
    {
        "name": "Valerina's Red Carpet Toes",
        "price": 26000,
        "size": 9,
        "style": "Scarpin",
        "onSale": false,
        "freeDelivery": false
    },
    {
        "name": "Neeta Parmar's Stylish Bees 'n' Honey Boots",
        "price": 10780,
        "size": 7,
        "style": "Wedge Booties",
        "onSale": true,
        "freeDelivery": true
    }
]

Shoe.insertMany(seedProducts)
    .then(d => console.log(d))
    .catch(err => console.log(err))
  • Run the MongoDB server
  • Open the terminal in the project directory path and run the seeds.js file:
$ node seeds.js
  • This is what our console prints:
(node:996) Warning: Accessing non-existent property 'MongoError' of module exports inside circular dependency
(Use `node --trace-warnings ...` to show where the warning was created)
(node:996) DeprecationWarning: Listening to events on the Db class has been deprecated and will be removed in the next major version.

CONNECTED TO MONGO!

[
  {
    _id: 616b18a556e71503e4a0334d,
    name: 'Mariana Grider Boots',
    price: 23000,
    size: 7,
    style: 'Slingbacks',
    onSale: false,
    freeDelivery: true,
    __v: 0
  },
  {
    _id: 616b18a556e71503e4a0334e,
    name: 'Purple Shimmery Heels',
    price: 13380,
    size: 6,
    style: 'Peep Toes',
    onSale: true,
    freeDelivery: false,
    __v: 0
  },
  {
    _id: 616b18a556e71503e4a0334f,
    name: 'Zac Porter Straps',
    price: 19760,
    size: 8,
    style: 'Ankle Strap',
    onSale: true,
    freeDelivery: true,
    __v: 0
  },
  {
    _id: 616b18a556e71503e4a03350,
    name: "Valerina's Red Carpet Toes",
    price: 26000,
    size: 9,
    style: 'Scarpin',
    onSale: false,
    freeDelivery: false,
    __v: 0
  },
  {
    _id: 616b18a556e71503e4a03351,
    name: "Neeta Parmar's Stylish Bees 'n' Honey Boots",
    price: 10780,
    size: 7,
    style: 'Wedge Booties',
    onSale: true,
    freeDelivery: true,
    __v: 0
  }
]

So, this is what our console prints! We have successfully connected to Mongo with the help of Mongoose. Now, we will check our MongoDB database of the documents. let us proceed with our guide on how to check if Mongoose is installed in Nodejs.

  • Now, open the Mongo shell to check if Mongoose was installed and is working properly
  • List out all databases and choose the one you just created:
show dbs
use shoeStore
  • Now, list all collections:
show collections
shoes
  • Use this collection because this is what Mongoose has instructed Mongo to create. We will now check if the documents were created or not:
> db.shoes.find({}).pretty()
{
        "_id" : ObjectId("616b18a556e71503e4a0334d"),
        "name" : "Mariana Grider Boots",
        "price" : 23000,
        "size" : 7,
        "style" : "Slingbacks",
        "onSale" : false,
        "freeDelivery" : true,
        "__v" : 0
}
{
        "_id" : ObjectId("616b18a556e71503e4a0334e"),
        "name" : "Purple Shimmery Heels",
        "price" : 13380,
        "size" : 6,
        "style" : "Peep Toes",
        "onSale" : true,
        "freeDelivery" : false,
        "__v" : 0
}
{
        "_id" : ObjectId("616b18a556e71503e4a0334f"),
        "name" : "Zac Porter Straps",
        "price" : 19760,
        "size" : 8,
        "style" : "Ankle Strap",
        "onSale" : true,
        "freeDelivery" : true,
        "__v" : 0
}
{
        "_id" : ObjectId("616b18a556e71503e4a03350"),
        "name" : "Valerina's Red Carpet Toes",
        "price" : 26000,
        "size" : 9,
        "style" : "Scarpin",
        "onSale" : false,
        "freeDelivery" : false,
        "__v" : 0
}
{
        "_id" : ObjectId("616b18a556e71503e4a03351"),
        "name" : "Neeta Parmar's Stylish Bees 'n' Honey Boots",
        "price" : 10780,
        "size" : 7,
        "style" : "Wedge Booties",
        "onSale" : true,
        "freeDelivery" : true,
        "__v" : 0
}

Great! Mongoose has successfully created and inserted a set of documents into our database. this proves Mongoose is successfully installed and is working properly.

Hope this guide on how to check if mongoose is installed in your Nodejs application, was helpful!

Read More: Monk vs Mongoose for MongoDB: Instant Comparative Guide

Conclusion

Not sure if Mongoose is installed? Here’s how to check if Mongoose is installed or not inside a Nodejs application.