DEV Community

Cover image for JavaScript Modules in NodeJS
katongole Isaac
katongole Isaac

Posted on

JavaScript Modules in NodeJS

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

const favEditor = "Visual Code";
let favLang = "JavaScript";
exports.community = function(){
    return "dev.to";
}
module.exports.language = favLang;
Enter fullscreen mode Exit fullscreen mode

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:
exports.variableName = "someValue";
module.variableName = "someValue";
Enter fullscreen mode Exit fullscreen mode

When we say export, we mean we are adding the variable to

 module.exports
Enter fullscreen mode Exit fullscreen mode

object and in this case we have added variableName to_ module.exports_ object.
To verify this, you can do

console.log(module.exports)
Enter fullscreen mode Exit fullscreen mode

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

const mod = require('./module1.js');
console.log(`Language: ${mod.language}`);
console.log(`community: ${mod.community()}`);
console.log(`favourite Editor: ${mod.favEditor}`);
Enter fullscreen mode Exit fullscreen mode

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

Top comments (0)