module.exports
module.exports
is the object that’s actually returned as the result of arequire
call.
Single quotes error: Trying to export:
module.exports = {
mongoURI:
`mongodb+srv://jim:xxxx@cluster0.dq6lk.mongodb.net/MERN?retryWrites=true&w=majority`,
};
Tossed the error: MongooseError: The
uriparameter to
openUri()must be a string, got "undefined"
which was fixed by double quotes instead of back tacks around the mongURI value.
ES6 imports
export const mongoURI =
"mongodb+srv://jim:xxx@cluster0.dq6lk.mongodb.net/MERN?retryWrites=true&w=majority";
`import { mongoURI } from “./config/keys”` Did not work at all. Troubleshooting forever. Finally realized I needed a `.js` file extension.
`import { mongoURI } from “./config/keys.js”` Gatsby does it automatically, so I was used to not including it. Now I wish it did not! Would have saved some time.
Nodemon
nodemon is a tool that helps develop node.js based applications by automatically restarting the node application when file changes in the directory are detected.
nodemon does not require any additional changes to your code or method of development. nodemon is a replacement wrapper for
node
. To usenodemon
, replace the wordnode
on the command line when executing your script
Cool little NPM package, no more restarting the server to see changes!
Just install, `npm i nodemon`, add to your scripts in your package.json file, and use `npm run server` instead of `npm run start`
{
"name": "mernshop",
"version": "1.0.0",
"description": "shopping list",
"main": "server.js",
"type": "module",
"scripts": {
"start": "node server.js",
"server": "nodemon server.js"
},
}
Mongoose
Mongoose provides a straight-forward, schema-based solution to model your application data. It includes built-in type casting, validation, query building, business logic hooks and more, out of the box.
Well I was still confused, so I had to search for more. Stackoverflow returned this:
I assume you already know that MongoDB is a NoSQL database system which stores data in the form of BSON documents. Your question, however is about the packages for Node.js.
In terms of Node.js, mongodb is the native driver for interacting with a mongodb instance and mongoose is an Object modeling tool for MongoDB.
Mongoose is built on top of the MongoDB driver to provide programmers with a way to model their data.
Main advantage is abstraction over pure mongo.
Many developers who come from SQL database types feel very uncomfortable working with dynamic collections that have no structure defined. So Schemas in the first place helps with that.
Additionally, it implements validation and other neat features in order to make sure your schema is consistent when inserting/updating/finding documents from collections.It also creates Model abstraction which makes it easier to work with, so it looks like you are working with just objects rather than pure data.
Mongoose : object data modeling (ODM) library that provides a rigorous modeling environment for your data. Used to interact with MongoDB, it makes life easier by providing convenience in managing data.
Mongodb: native driver in Node.js to interact with MongoDB.