How to Change the Node.js Module Wrapper ? Last Updated : 26 Jun, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report Changing the Node.js module wrapper involves customizing the way modules are wrapped by modifying the Module.wrap method. This allows for altering the function wrapper used in module loading. Module Wrapper FunctionUnder the hood, NodeJS does not run our code directly, it wraps the entire code inside a function before execution. This function is termed as Module Wrapper Function. Before a module's code is executed, NodeJS wraps it with a function wrapper that has the following structure: (function (exports, require, module, __filename, __dirname) { //module code});Use of Module Wrapper Function in NodeJSThe top-level variables declared with var, const, or let are scoped to the module rather than to the global object.It provides some global-looking variables that are specific to the module, such as: The module and exports object that can be used to export values from the module.The variables like  __filename and __dirname, that tell us the module's absolute filename and its directory path.Modifying Module Wrapper FunctionConsider that we have two files, main.js and module.js. In main.js we overwrite the Module.wrap function in order to console.log('modifedMWF'); every time a module is required. Now if we require module.js, it contains a message to confirm whether our modifications are successful or not. Node // main.js var Module = require("module"); (function (moduleWrapCopy) { Module.wrap = function (script) { script = "console.log('modifiedMWF');" + script; return moduleWrapCopy(script); }; })(Module.wrap); require("./module.js"); Node // module.js console.log("Hello Geeks from module.js!"); Output: Running main.js, we get the following output that confirms our successful alteration in Module Wrapper Function. node main.jsOutput window on running main.js Comment More infoAdvertise with us Next Article How to Change the Node.js Module Wrapper ? S stewie77 Follow Improve Article Tags : Web Technologies Node.js Technical Scripter 2020 NodeJS-Questions Similar Reads 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 use Superheroes Module in Node.js ? The superheroes module is a fun and simple Node.js package that allows you to generate random superhero names. It's a great example of how to use third-party modules in Node.js and can be useful for testing, generating sample data, or just adding some flair to your applications. In this article, we' 2 min read How to Change npm start Script of Node.js ? In Node.js, npm (Node Package Manager) provides a convenient way to manage project scripts through the scripts field in the package.json file. By default, the npm start command is used to start your Node.js application. However, you might need to customize this command to suit specific requirements, 3 min read What are modules in Node JS ? In NodeJS, modules are encapsulated units of code that can be reused across different parts of an application. Modules help organize code into smaller, manageable pieces, promote code reusability, and facilitate better maintainability and scalability of NodeJS applications. Types of Modules:Core Mod 2 min read Like