What is going on when we do require(‘some module’)? What if I want to include the MySQL module to my a few modules and use the same connection, how to have we include it properly? In this article, we’ll try to figure out how the node.js modules work.
#How to make your own module
Create a new module you have to create a separated file. For an example, we’ll be using an m1.js file here is its code
m1.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
class classM1{ constructor(){ console.log('construct M1'); } } var printStr = () => { console.log('Hello world!'); } const obj = { val1: 1, val2: 2, } module.exports = { classM1: classM1, printStr: printStr, obj: obj } |
module.exports statement is used for export something it could be any value like variable, function, object. In our case, I used an object with three properties: classM1, printStr and obj. Now let’s look at index.js
index.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
const m1 = require('./m1') console.log(m1); // output // { classM1: [Function: classM1], // printStr: [Function: printStr], // obj: { val1: 1, val2: 2 } } // we can call all of them // 1. create a new object by using class const obj = new m1.classM1() // construct M1 // 2. call a function m1.printStr() // Hello world! // 3. use an object console.log(m1.obj.val1) // 1 console.log(m1.obj.val2) // 2 |
here we include the m1.js file using const m1 = require(‘./m1’). We can omit a .js extension. Now, m1 has the object of our module and we can use all its properties.
#How to make a reference module and what are node.js reference modules?
A reference module is a module that has inside an object or some data that could be referenced through all the app. Let’s overview the code.
index.js
1 2 3 4 5 6 7 8 9 10 |
let m1 = require('./m1') // return an object console.log(m1); // { val: 100 } m1.val = 200 // assign 200 let m2 = require('./m2') m2() // change m1 by using m2 console.log(m1); // now here is 300 |
m1.js
1 2 3 4 5 |
let obj = { val: 100 } module.exports = obj |
m2.js
1 2 3 4 5 6 7 8 9 |
let m1 = require('./m1') console.log("m1 = ", m1); let change = () => { m1.val = 300 } module.exports = change |
her is two modules m1 and m2. m1 returns an object with property {val: 100}. m2 returns a function that changes the val of the m1’s object. That’s it. as you can see we can use m1 module anywhere in our app by using require(‘./m1’)
In this way, we can create an object that will have a MySQL connection and we will use it anywhere in our modules
#How to use global module scope
So in module global scope the variable will available to all users Look at this code
1 2 3 4 5 6 7 8 9 10 11 |
let value = 10 // it will be the reference in the valuePlusFive function const valuePlusFive = () => { // the output depends on how many times the function is colled // if the function was called 5 times the value will be 10 + 5 * 5 = 35 and so on console.log('value = ', value); value += 5 return { value } } module.exports = valuePlusFive |
the value variable will accessible to each client, so if you don’t want such a behavior you have to define the variable inside the function like that
1 2 3 4 5 6 7 8 |
const valuePlusFive = () => { let value = 10 console.log('value = ', value); // it will always be 10 value += 5 return { value } } module.exports = valuePlusFive |
Here is some example with puppeteer How to close the browser properly that is show how to use
the end