Create a backend in Javascript: NodeJS Module System

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 the…


This content originally appeared on DEV Community and was authored by Eric The Coder

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: https://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 gretting = function (name) {
    console.log (`Hello $ {name}, welcome to NodeJS`)
}

gretting (name)

It could be interesting to make the greeting module reusable. To do this we will place it in its own file called gretting.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 ('./ gretting.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: https://twitter.com/EricTheCoder_ to be notified of the publication of the next article (within two days).


This content originally appeared on DEV Community and was authored by Eric The Coder


Print Share Comment Cite Upload Translate Updates
APA

Eric The Coder | Sciencx (2021-09-30T11:32:43+00:00) Create a backend in Javascript: NodeJS Module System. Retrieved from https://www.scien.cx/2021/09/30/create-a-backend-in-javascript-nodejs-module-system/

MLA
" » Create a backend in Javascript: NodeJS Module System." Eric The Coder | Sciencx - Thursday September 30, 2021, https://www.scien.cx/2021/09/30/create-a-backend-in-javascript-nodejs-module-system/
HARVARD
Eric The Coder | Sciencx Thursday September 30, 2021 » Create a backend in Javascript: NodeJS Module System., viewed ,<https://www.scien.cx/2021/09/30/create-a-backend-in-javascript-nodejs-module-system/>
VANCOUVER
Eric The Coder | Sciencx - » Create a backend in Javascript: NodeJS Module System. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/09/30/create-a-backend-in-javascript-nodejs-module-system/
CHICAGO
" » Create a backend in Javascript: NodeJS Module System." Eric The Coder | Sciencx - Accessed . https://www.scien.cx/2021/09/30/create-a-backend-in-javascript-nodejs-module-system/
IEEE
" » Create a backend in Javascript: NodeJS Module System." Eric The Coder | Sciencx [Online]. Available: https://www.scien.cx/2021/09/30/create-a-backend-in-javascript-nodejs-module-system/. [Accessed: ]
rf:citation
» Create a backend in Javascript: NodeJS Module System | Eric The Coder | Sciencx | https://www.scien.cx/2021/09/30/create-a-backend-in-javascript-nodejs-module-system/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.