Working with modules in NodeJs
So far, we have seen that Node provides us with some Global modules and some Pseudo Global modules, and we can use modules written by others by installing packages from npm store.
What this means is we can utilise module system in three ways:
1. Use Global Modules in our Project directly.
2. Use core node modules- using require(‘<module-name>’).
3. Use our own modules within project- using require(‘<module-name>’) and module.exports
4. Use module from npm store - running npm install <package-name> and then requiring it using require(‘<package-name>’)
If you are new to NodeJs, the 3rd way might need some clarity.
When we are working on a project, it can happen that we require a part of code from one module inside another. To do this, we need to first expose the desired code from that module using module.exports(which is a global ) and then require it, where it is needed by specifying the path of the file from where it was exported from.
module is a variable that represents the current module, and
exports is an object that will be exposed as a module. So, whatever you assign to
module.exports will be exposed as a module.
What is the utility of a package .json file?
A package.json file is a human readable meta-data about your project in JSON format located at the root of project. It contains important details like, author, name , version, description, scripts, engines and dependencies if any.
When is it created?
We will mostly need a package.json file before we can use any other package from npm. However, even if we are not using external packages it is wise to create a package.json file, as it will help us to manage versions and also allow us to publish our package to npm in future.
How to create it?
We can use the command npm init or npm init -y , to create package.json file. If we use npm init , it will ask for few details about our project, but if we want to fast forward to a auto-filled package file we can use npm init -y.
Now, whenever we install a package, it will get added to package.json file as dependencies and a npm_modules folder will get created.
Utility of this file?
As this has already been mentioned, this file contains meta-data related to our project in json format. It sets the entry point to our project using
main key. We can make our package private using
private key. Using
devDependencies we can set scripts to run, define package dependencies, and developement dependencies respectively. The
version key requires a more in depth explaination, so we will cover it in a later article (Semantic versioning in Node.Js). For now, let’s talk about the most common benefit of a package.json file.
In projects, it is a good practice to use .gitignore file to ignore npm_modules before pushing our code to remote repository. It is because npm_modules folder has a big size, and copying it takes a lot of time and unnecessary space in remote. So , these files are ignored and not uploaded. Now, if we clone a project and it does not have npm_modules that it needs to run, we are going to face errors and the project won’t run. Why? Because it is not able to find the required packages in node_modules.
This is where package.json file comes to our rescue. As it already has a list of dependencies, we just have to run npm install. And it will download the required packages automatically using the contents from package.json and package-lock.json. (the lock file is used to ensure the integrity and sanity of the installed modules)
A note about package-lock.json from Node Docs:
“The goal of
package-lock.json file is to keep track of the exact version of every package that is installed so that a product is 100% reproducible in the same way even if packages are updated by their maintainers.”
In the next article, we will start with two important core node modules: the file system module and the http module. Hope to see you there!