mkdir hello-modules
cd hello-modules
npm init -y
package.json
{
"name": "hello-modules",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}
now create two new files
first one -> hello-modules/index.js
is it necessary to create a file with the same name mentioned in package.json
"main": "index.js"
? NO.
If you are not going to publish it to npm using npm publish
This field "main"
does not serve any purpose.
second one -> hello-modules/universe.js
hello-modules/index.js
console.log('hello modules from index.js')
node index.js
*make sure your terminal is in current directory โฆ\hello-modules>
hello modules from index.js
Woohoo, your javascript code now runs in the terminal.
Thatโs why Node.jsยฎ is called JavaScript runtime.
module.exports
& require()
Let me clear you a few things before we go ahead
When you start doing cool stuffs with javascript in terminal instead of browser.
you write javascript code in file like index.js
, that file is known as a module.
Now there are two types of modules
by default node supports Commonjs module system
you can communicate between two files ( modules ) and use one another codes, functions, classes
hello-modules/universe.js
// declare a const
const milkyWay = " it has billions of stars "
// and export it with a magic spell
module.exports = milkyWay;
hello-modules/index.js
// magic spell to get that const from another file (module)
// is to make that module (file) available in this file first
const milkyWaySomeWhereElse = require('./universe.js');
// now console it to terminal
console.log(milkyWaySomeWhereElse);
node index.js
it has billions of stars
Congrats ๐, You just used two magical spells
exports
andrequire
hello-modules/universe.js
& make it empty hello-modules/index.js
write
const milkyWaySomeWhereElse = require('./universe.js');
console.log(milkyWaySomeWhereElse);
node index.js
{}
hello-modules/universe.js
write
const solarSystem = "It has a planet called Earth";
const pandora = "this planet exists in movie Avatar";
module.exports.solarSystem = solarSystem;
module.exports.pandora = pandora;
// you can remove module. from above lines
// exports.solarSystem = solarSystem;
// module.exports.pandora = pandora;
// they will work the same way
hello-modules/index.js
keep it same
const milkyWaySomeWhereElse = require('./universe.js');
console.log(milkyWaySomeWhereElse);
node index.js
{
solarSystem: 'It has a planet called Earth',
pandora: 'this planet exists in movie Avatar'
}
if we export from a module (file) like
module.exports.solarSytem = solarSystem
module.exports.pandora = pandora
const milkyWaySomeWhereElse = require('./universe.js')
pandora
and solarSystem
// either by dot Notation
const pandora = milkyWaySomeWhereElse.pandora;
const solarSystem = milkyWaySomeWhereElse.solarSystem;
// or from bracket Notation
const pandora = milkyWaySomeWhereElse["pandora"];
const solarSystem = milkyWaySomeWhereElse["solarSystem"];
// or by destructuring assignment
// most popular one used in modules while importing
const { pandora, solarSystem } = milkyWaySomeWhereElse;
// most of the times you will see
const { pandora, solarSystem } = require('./universe.js');
hello-modules/universe.js
write
const solarSystem = "It has a planet called Earth";
const milkyWay = " it has billions of stars "
module.exports.solarSystem = solarSystem;
module.exports = milkyWay;
hello-modules/index.js
same
const milkyWaySomeWhereElse = require('./universe.js');
console.log(milkyWaySomeWhereElse);
node index.js
it has billions of stars
in hello-modules/universe.js
module.exports = milkyWay;
only this line worked and milkyWay got exported
module.exports.solarSystem = solarSystem
useless hello-modules/universe.js
)
using module.exports = anything
. hello-modules/index.js
like const milkyWaySomeWhereElse = require('./universe.js');
console.log(milkyWaySomeWhereElse);
hello-modules/universe.js
module.exports = "you can export a string";
milkyWaySomeWhereElse
will be equal to "you can export a string"
module.exports = ["an", "array", "with", 1, "boolean", "value", true];
now milkyWaySomeWhereElse
will be equal to ["an", "array", "with", 1, "boolean", "value", true]
module.exports = {
sky:"it is blue",
hello:"you can export a object"
};
In this case milkyWaySomeWhereElse
will be equal to
{
sky:"it is blue",
hello:"you can export a object"
}
module.exports = function(){
return "you can a export a function which returns some value";
}
// or
// module.exports = function(){
// console.log("you can a export a function which do not returns any value");
// }
milkyWaySomeWhereElse
is a function now
the function is declared only and get assigned to another variable,
but not called yet!
or not executed yet
This function is same as normal functions in javascript,
to call or execute write </br>
milkyWaySomeWhereElse()
console.log(milkyWaySomeWhereElse())
will output the string
"you can a export a function which returns some value"
hello-modules/universe.js
you can export a class. class Planet {
constructor(name) {
this.name = name;
}
getName() {
return this.name;
}
sayHiTo(name) {
console.log(`Hi ${name}, I'm ${this.name}`);
}
}
module.exports = Planet;
hello-modules/index.js
const Planet = require('./universe.js');
const mars = new Planet('Mars');
console.log(mars.getName());
mars.sayHiTo('Elon Musk');
node index.js
Mars
Hi Elon Musk, I'm Mars
NOTE: I will write about ECMAScript (ES6) modules in next post.