Kselax.ru

Hacker Kselax – the best hacker in the world

Menu
  • Blog
  • Contacts
  • wp plugin generator
  • English
    • Русский
Menu

How to work with node.js modules

Posted on 17 November, 201813 February, 2019 by admin

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

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Categories

  • bash (1)
  • English (9)
  • JavaScript (4)
  • node.js (22)
  • photoshop (1)
  • php (3)
  • React (9)
  • sclerotic (6)
  • Ubuntu (10)
  • Uncategorized (13)
  • Wordpress (1)

Tags

Ajax apache2 automation bash chrome-extension command line editor ejs email English English-grammar framework functions git graphql handlebars hybrid app installation javascript js linux newbie node.js node.js javascript nodemailer npm objects Performance php phpmyadmin playonlinux promise rabbitmq React react-router redis reverse-proxy session shell socket.io sublime text 3 time zones ubuntu unity webpack

Recent Comments

  • damien on How to install npm and nodejs the latest versions on ubuntu
  • Cam on How to install npm and nodejs the latest versions on ubuntu
  • Pierre on socket.io with apache as a reverse proxy on the CentOS
  • admin on How to use react-router with a few languages
  • admin on How to install npm and nodejs the latest versions on ubuntu
©2021 Kselax.ru Theme by ThemeGiant