JavaScript Modules in NodeJS
Published: 2022-03-02 11:42:01

 > Greetings loving friends out there, i hope everything is going well, sorry to others that are facing challenges but i know one day we shall all win race. If we believe and work towards achieving the desired goals, we have already won before we start. Today i revised about JavaScript modules and i wanted to share this to others though the concepts might have been published earlier on the internet. 

*let's start off...*
Well, nothing new i will discuss today because i have a feeling that this may not be new to some of you. The discussion is just about **files**.
_defn:_
**file** - can be a container of text, images, code and many more.
 In a Programming context, we can use the _module_ to mean _file_ , yeah!! not to keep ourself backwards, we also want to move with what's trending right?
Now lets us talk in terms of JavaScript specfically.
>Don't forget!!!, a module is a file. so we can define a module in a JavaScript setting.

_defn:_
**module** is a file containing javascript code in any javascript project(nodejs project). Think of a project as a directory. forexample _Me_ is a directory and _you_ is a module, so now we can use relative path to locate our module like this :- 
`Me/you` on linux and Mac  or `Me\you` on Windows.
Now you have grasped the concept of a module. Thats greats!!
In JavaScript, modules have the file extensions suchas 
- .js .cjs ,.mjs, .node
The _.js_ and _c.js_ modules are for commonJS modules and .mjs and .node modules are for ES6 modules in Nodejs.
I will illustrate the use of both ES6 and commonJS modules.
**commonJS modules**
Our main emphasis here is to see how we can import and export code situated in the modules.
lets create our first module and name it module1.js. we type in some interesting javaScript code

**Exporting**
_file : module1.js_
```javascript
const favEditor = "Visual Code";
let favLang = "JavaScript";
exports.community = function(){
    return "dev.to";
}
module.exports.language = favLang;
```
In the module above, 
* the first line declares and initialize a constant variable _favEditor_ to a string primitive value "Visual Code".
* the second line, also does the same though it is not a constant.
Now things become more interesting on line 3. Let us go slowly, in Nodejs we have a keyword _module_ which is an object with many useful properties and other objects. Inside the module object, we have _exports_ object which possesses everything we export. To export a variable  we do:
```javascript
exports.variableName = "someValue";
module.variableName = "someValue";
```
When we say export, we mean we are adding the variable to ```javascript
 module.exports
``` 
object and in this case we have added _variableName_ to_ module.exports_ object.
To verify this, you can do 
```javascript 
console.log(module.exports)
```
Now you know how to export commonJs modules in NodeJs. lets learn how to import modules.
**Importing**
lets create another module and name it module2.js
_file: module2.js_
```javascript
const mod = require('./module1.js');
console.log(`Language: ${mod.language}`);
console.log(`community: ${mod.community()}`);
console.log(`favourite Editor: ${mod.favEditor}`);
```
For imports, in commonJs modules we use require() function which is synchronous (we have to wait till the contents from _module1.js_ are loaded and referenced by a constant _mod_)
 we have used the _mod_ to have access to whatever is exported in the _module1.js_ module. 
 * On line 2, the output is `JavaScript`
 * On line 3, a call to a function community is made and the output is `dev.to`

* On line 4, output is `undefined`. because the _favEditor_ variable is private to the module (not included to module.exports object)

Thanks, for ES6 modules, you can use this github repository, it has some practical examples for both commonJs and ES6 modules. [repository](https://github.com/katongole-isaac/nodejsModules)