Mastering Debugging in Node.js with Node Inspector
Explore Node Inspector, a powerful GUI-based tool for debugging Node.js applications. Learn how to install, set up, and use Node Inspector for efficient debugging, and discover why the built-in Node.js debugger may be the better choice for modern development.
Export Module in Node.js
Learn how to expose different types as modules using module.exports
in Node.js. This special object allows you to export functionalities from your module.
Export Literals
Export a simple string literal:
Message.js
module.exports = 'Hello world';
Output
Hello world
Export Object
Export an object with properties:
Message.js
module.exports = {
greeting: 'Hello world'
};
Output
Hello world
Export Function
Export a function:
Log.js
module.exports = function (msg) {
console.log(msg);
};
Output
Hello world
Export Function as a Class
Export a function that acts like a class:
Person.js
module.exports = function (firstName, lastName) {
this.firstName = firstName;
this.lastName = lastName;
this.fullName = function () {
return this.firstName + ' ' + this.lastName;
};
};
Output
James Bond
Load Module from a Folder
Import a module from a subfolder:
app.js
var log = require('./utility/log.js');
Output
Log module loaded
For a folder with a package.json
file, Node.js uses the main
entry to find the module. If the package.json
is missing, it defaults to index.js
.