How to use External Modules and NPM in a project ? Last Updated : 28 Apr, 2025 Comments Improve Suggest changes Like Article Like Report Need for External Modules: For a large JavaScript application, it becomes difficult and messy to write the whole code in just one JavaScript file. This is where CommonJS comes into the picture and this CommonJS format defines a module format that can be used up for breaking your JS application into multiple files. Node.js adopts this CommonJS format for organizing our JS application into multiple files. Within this Node.js application, we have the module.exports property which is used to determine the export from the current module. Types of Node modules: Module in Node.js is a simple or complex functionality organized into single or multiple JS files which can be used again throughout the Node.js application. There are three types of Node.js modules:  Local/File-based modules: It define the Node modules within a file in our application and is used within our application.Core modules: The core modules are inbuilt modules in Node.js. These modules provide sufficient functionality so that external module designers can add in their own functionality that can be used while developing Node applications. The core modules include path, file system, os, util, and a few others.Third-party modules: Third-party modules are the external Node modules. These are the third-party Node modules developed by Node developers that are made available through the Node ecosystem. But we need a package manager that maintains all the modules so that they can be accessed with ease. This is where NPM comes into the picture. NPM (Node Package Manager): NPM is the default package manager for JavaScript runtime environment in Node.js. The Node.js Package Manager (npm) is the default and most popular package manager in Node.js ecosystem that is primarily used to install and maintain external modules in Node.js application. Users can basically install the node modules needed for their application using npm. How to export Modules ? First, initialize a node.js application by typing in npm init in the command prompt/terminal (make sure you are present in the current project folder). It will create a package.json file.Use the following syntax to add a module in Node.js project. Syntax:  var module = require("module_name"); Creating own modules and using it: First, initialize node in a directory by typing npm init in the command prompt/terminal.  Creating a Node.js module (rectange.js):  javascript module.exports = (length, breadth, callback) => { if (length <= 0 || breadth <= 0) setTimeout(() => callback(new Error( "Dimensions cannot be negative: length = " + length + ", and breadth = " + breadth), null), 5000); else setTimeout(() => callback(null, { Perimeter: () => (2*(length+breadth)), Area:() => (length*breadth) }), 5000); } Using the Node module in your application (module.js):  javascript var rect = require('./rectangle'); module.exports.Rect = function Rect(l, b) { rect(l, b, (err, rectangle) => { if (err) console.log("There is an ERROR!!: ", err.message); else { console.log("Area of rectangle with dimensions length = " + l + " and breadth = " + b + " : " + rectangle.Area()); console.log("Perimeter of the rectangle with dimensions length = " + l + " and breadth = " + b + " : " + rectangle.Perimeter()); } console.log("\n\n"); }); }; Initializing the main file (index.js):  javascript var rect = require("./module"); rect.Rect(5, 2); rect.Rect(-1, 0); rect.Rect(12, 4); rect.Rect(8, 6); rect.Rect(3, 4); Output:   Comment More infoAdvertise with us Next Article How to use External Modules and NPM in a project ? J Jasraj Follow Improve Article Tags : Web Technologies Node.js Node.js-Misc Similar Reads How to Create and Run a Node.js Project in VS Code Editor ? Visual Studio Code (VS Code) is a powerful and user-friendly code editor that is widely used for web development. It comes with features like syntax highlighting, code suggestions, and extensions that make coding easier. In this article, we'll show you how to quickly create and run a Node.js project 2 min read How To Use Node Modules with npm and package.json NodeJS is a powerful runtime for server-side JavaScript & these modules are reusable pieces of code that can be easily imported and used in NodeJS applications. npm (Node Package Manager) is the default package manager for Node JS and is used to install, manage, and publish NodeJS packages. This 3 min read How to use EcmaScript Modules in Node.js ? Using ECMAScript Modules (ES Modules or ESM) in Node.js allows you to take advantage of modern JavaScript syntax for organizing and managing your code. ECMAScript Modules provide a more structured and standardized way to work with modules compared to CommonJS, which has been traditionally used in No 2 min read How To Create Modules in NodeJS? Modules are the building blocks of NodeJS code. They help you break down your project into smaller, manageable pieces, each with its own job.To create a module, just make a JavaScript file and export what you want to share. Other files can then import and use those exports, adding that functionality 3 min read How to add new functionalities to a module in Node.js ? Node.js is an open-source and cross-platform runtime environment built on Chromeâs V8 JavaScript engine for executing JavaScript code outside of a browser. You need to recollect that NodeJS isnât a framework, and itâs not a programming language. In this article, we will discuss how to add new functi 3 min read Like