Eric The Coder
Eric The Coder Blog

Eric The Coder Blog

Create a backend in Javascript (part 2): NodeJS Module System

Create a backend in Javascript (part 2): NodeJS Module System

Eric The Coder's photo
Eric The Coder
·Sep 30, 2021·

3 min read

Here is a series of articles to help you create backend applications in Javascript.

Node.js is now a must, so it is essential for a developer to master it.

I will publish a new article every two days and little by little you will learn everything there is to know about Node.js

To not miss anything follow me on twitter: twitter.com/EricTheCoder_


NodeJS Module System

Writing code in a file is fine, but if your application need a lot of code, your file will quickly become too large.

This is why it is better to separate your code into several modules (file) in order to make the code reusable and much better structured.

here is an example

app.js

const name = 'Mike Taylor'

const greeting = function(name) {
    console.log(`Hello ${name}, welcome to NodeJS`)
}

greeting(name)

It could be interesting to make the greeting module reusable. To do this we will place it in its own file called greeting.js

const greeting = function(name) {
    console.log(`Hello ${name}, welcome to NodeJS`)
}

By default NodeJS does not allow to use this function from another module. To do this, you must indicate to the module which elements can be exportable:

const greeting = function(name) {
    console.log (`Hello ${name}, welcome to NodeJS`)
}

module.exports = greeting

Note here the last line 'module.exports = greeting', this function allows the use of the greeting function from another module.

From app.js you can now load this module with the 'require' function

const greeting = require('./greeting.js')

const name = 'Mike Taylor'
greeting(name)

The 'require' function will create a reference with the greeting module and place this reference in the const greeting variable (this variable could have been called another name than greeting)

Note that the function require ('./greeting.js') uses the path './' this allows to indicate to NodeJS that the module is in the same folder as our app.js file

Multiple exports

It is possible to export several elements with the function module.exports. Here is an example: person.js

const name = 'Mike Taylor'
const car = 'Ford Mustang'

module.exports = {name, car}

Multiple exports is therefore done with an object that contains several elements.

const person = require('./ person.js')

console.log(person.name, person.car)

Note that the 'person' variable does not point to the 'name' or 'car' directly, it points to the object that is exported. So to return its content we have to use 'person.name'

Multiple export (alternative syntax)

It is possible to export several elements with the function module.exports. Here is an example: person.js

const name = 'Mike Taylor'
const car = 'Ford Mustang'

module.exports.name = name
module.exports.car = car

The usage remains the same:

const person = require('./ person.js')

console.log(person.name, person.car)

It is also possible to use deconstruction

const {name, car} = require('./ person.js')

console.log(name, car)

The 'require' function executes the module

When executing the require function, the module is executed immediately. here is an example

// hello.js

const hello = function() {
    console.log('Hello World')
}

modules.exports = hello
// app.js

const hello = require('./ hello.js')

As soon as NodeJS executes this line, the hello module is also executed. In this example the module only does an export but if the module contained code it would be executed, here is an example

// hello.js

const hello = function() {
    console.log('Hello World')
}

console.log('Hello Node!')

modules.exports = hello
// app.js

const hello = require('./ hello.js')

Hello()

If you launched app.js, you will see that it will display 'Hello Node!' before the 'Hello World' because as mentioned, the 'require' executes the module.

Take this fact into account when you create your modules in order to avoid unwanted behavior.

Conclusion

That's all for today, follow me on twitter: twitter.com/EricTheCoder_ to be notified of the publication of the next article (within two days).

Did you find this article valuable?

Support Eric The Coder by becoming a sponsor. Any amount is appreciated!

Learn more about Hashnode Sponsors
 
Share this