The Node.js ecosystem is huge, and it’s hard to keep up with everything. Modules are a great way to organize your projects and share code across projects. In a nutshell, modules are self-contained units of program or functionality that can be shared or reused across the projects.
Nodejs comes with plenty of in-built modules. And NPM provides a huge library of free modules, but it can be difficult to find the right one for your project. With Modules, you can search and filter modules by category, popularity, or just by browsing through the list of available modules.
Two things you should be familiar with when dealing with the modules.
- Installing and Loading modules
- Create new module
Installing & Loading modules
By default, node provides core modules. We can install third-party modules from the npm registry (https://npmjs.org), and we use the command-line tool to install the modules. Command-line tools can be used to search and manage modules too.
Once you know the name of the module, type the below command in the command line to install it,
$ npm install module-name
// Ex: npm install express
Modules can also be installed globally, which means installing them into the global modules folder (/usr/local/lib/node_modules)
$ npm install -g module-name
// Ex: npm install -g express
After installing a module, it can be loaded with require(‘module_name’)
When you share your project with others, and since they have to use the same module in their local environment, you should consider adding that particular module into package.json.
mymac@Pro test % npm install express
+ express@4.17.3
added 50 packages from 37 contributors and audited 50 packages in 2.965s
found 0 vulnerabilities
mymac@Pro test % node
Welcome to Node.js v14.16.0.
Type ".help" for more information.
> var express = require("express");
undefined
> typeof express
'function'
Create and manage modules
In nodejs, we can create custom modules and distribute them to others. These modules can be included in your nodejs application.
To create and distribute a custom module, we need to export it using the “exports” keyword. When we use the keyword “exports” it ensures that the functionality in the module/file can be accessed in other files.
Now let’s create a user module and export it to use in some other file.
// user.js
const getUserName = () => {
return "buddysuri"
}
exports.getUserName = getUserName
// index.js
const user = require(./user); // importing user module
console.log (`User name: ${user.getUserName}`) // User name: buddysuri
Let’s go through the above code,
In user.js we are defining a getUserName method, and by using the exports keyword, we are creating a module to import in other files to access the method (getUserName). Here we are importing index.js using require keyword.
Here we are exporting a single value/method. Can we export multiple methods or values? The answer is YES
// user.js
const getUserName = () => {
return "buddysuri"
}
const getUserLocation = () => {
return "Singapore"
}
exports.getUserName = getUserName
exports.getUserLocation = getUserLocation
// index.js
const user = require(./user); // importing user module
console.log (`${user.getUserName} lives in ${user.getUserLocation}`) // buddysuri lives in Singapore
There is another variation in export syntax, exporting the method on the go instead of exporting at the end of the file.
// user.js
exports.getUserName = () => { return "buddysuri" }
exports.getUserLocation = () => { return "Singapore" }
// index.js
const { getUserName, getUserLocation } = require('./user')
console.log (`${getUserName} lives in ${getUserLocation}`)
// It prints buddysuri lives in Singapore
Till now, we exported multiple values/methods and used the exports keyword for it. But when you have a module that exports a single thing like a class, we use module.exports keyword
// user.js
class User {
constructor (name, location) {
this.name = name
this.location = location
}
getUserData() {
return `${this.name} lives in ${this.location}`
}
}
module.exports = user
// index.js
const User = require('./user');
const user1 = new User('buddysuri', 'Singapore');
console.log(user1.getUserData());
// Prints buddysuri lives in Singapore
So what is the difference between exports & module.exports ?
module.exports will be used when we want to export a single class/value/method from one module to another. The require() returns an object reference.
exports will be used when we want to export multiple values/methods from one module to another.
0 Comments