Create a backend in Javascript (part 2): NodeJS Module System
Tags: javascript, node, beginners, tutorial
Published: 2021-09-30 11:32:43

---
title: Create a backend in Javascript (part 2): NodeJS Module System
published: true
description: 
tags: javascript, nodejs, beginner, tutorial
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1ph7yc1i1vqqgwpxegw5.png
series: nodejs
---

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_
<hr>

## 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

```jsx
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

```jsx
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:

```jsx
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

```jsx
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

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

module.exports = {name, car}
```

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

```jsx
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. <exportname> Here is an example: person.js

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

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

The usage remains the same:

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

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

It is also possible to use deconstruction

```jsx
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

```jsx
// hello.js

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

modules.exports = hello
```

```jsx
// 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

```jsx
// hello.js

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

console.log('Hello Node!')

modules.exports = hello
```

```jsx
// 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).