NPM stands for Node Package Manager. It serves as a free package registry accessible to anyone, making it easier to share javascript packages with others. Users usually interact with NPM in two ways: as someone downloading a package and someone publishing a JavaScript package.
A module is basically a JavaScript library which would contain a set of functions that you want to include in your code.
Node.js has many built-in modules that you can simply use without installing anything. A list of modules can be found here.
To use a module in your code, use the function require()
.
var http = require('http')
The require()
function reads a JavaScript file, executes it, and then returns the exports
object. The exports
keyword makes properties and methods available outside of the module file.
exports.myDateTime = function () {
return Date();
};
var dt = require('./myfirstmodule');
We use the ./
here to locate the module because the module is located in the same file as the Node.js file.