How to install mongoose with npm and import into NodeJs
Introduction
MongoDB and NodeJs are often used together for developing back-ends. MongoDB is a NoSQL database. To connect the database and manage relationships between data, the mongoose is used. It is an Object Data Modeling (ODM) library. It provides schema validation and also helps in managing relationships between data. In this article, we will discuss how to install mongoose with npm and import it into NodeJs.
Installing NodeJs and NPM
Mongoose is a package that is available in the Node Package Manager (NPM). To install mongoose, we first need to have NodeJs and NPM installed. So let’s install NodeJs. NPM automatically gets installed with it.
Go to https://NodeJs.org/.
Here, you will find two versions of Nodejs – LTS and the latest one.
Download accordingly. Our recommendation is the LTS version because it is the most supported one.
Wait for the download to finish and then click on the downloaded package.
Go through the installation steps and click “finish” when the installation is complete.
NPM automatically gets installed with Node.js. To make sure that installation was successful, open the terminal and type the following command.
1 | node - v; |
If the version of the node is displayed, then you are ready to go further.
As mentioned earlier, NPM automatically gets installed with NodeJs.
Installing express
Next, we need to install express. It will provide all the necessary node modules. But before installing express, we need a package.json file. So create a folder and name it whatever you like. Then, open a terminal and move to the newly created folder. Run the following command.
1 | npm init -y |
The next step is to install express.
1 | npm install express |
Installing and importing mongoose in Nodejs
To install mongoose, run the following command.
1 | npm install mongoose |
This will install the mongoose package. Then, create a new file and name it “index.js”. In this file, we will import mongoose.
Any package can be imported in a NodeJs file by using the require keyword and passing the package name in single quotes.
1 | var mongoose = require("mongoose"); |
This is how mongoose is imported in a NodeJs file.
Conclusion
This was a quick rundown of the steps needed to install mongoose and import it into NodeJS. If you’re uncomfortable using npm, don’t be intimidated. Npm is just a package manager much like yarn. It’s very easy to use as you saw, npm install mongoose
and that’s it! We also used the require statement to import the library into the application. The require statement will look through your installed libraries and find the mongoose library if it’s installed.
For setting up mongoose models, please see their documentation at https://mongoosejs.com/. Their getting started examples are a great place to start.
Thank you for joining us for this overview of how to install and import mongoose into a NodeJS application with the use of the node package manager npm.
Pilot the ObjectRocket Platform Free!
Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.
Get Started