Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,870 questions

51,793 answers

573 users

What is the module wrapper function in Node.js

1 Answer

0 votes
// Before a module code is executed, Node.js will wrap it with a function wrapper that 
// provide objects (module and exports) and variables (__filename and __dirname) 
// to use in your module code.

// Module Wrapper Function 
(function (exports, require, module, __filename, __dirname) {

    // Your module code

})


// Example: (You don't need to write the module wrapper function in your code)

(function (exports, require, module, __filename, __dirname) {

    console.log(__filename);    
   
    class Worker {
        constructor(name, age) {
            this.name = name;
            this.age = age;
        }
        show() {
            console.log(`name = ${this.name} age = ${this.age}`);
        }
    }

    module.exports = Worker;

})

 



answered Mar 9, 2020 by avibootz
edited Mar 9, 2020 by avibootz

Related questions

1 answer 190 views
4 answers 329 views
1 answer 87 views
2 answers 157 views
1 answer 130 views
1 answer 115 views
2 answers 125 views
...