Episode 04
Episode 04
These two files, app.js and xyz.js, have different code that is not related to each
other, so in NodeJS we call them separate modules.
Steps:
3. Then, run the code using Node.js. (As discussed in the last lecture, I hope you
have revised it well .) 🙄
Ans:
module.exports
you can omit the .js extension, and it will still work correctly. Node.js
automatically resolves the file extension for you.
summary: To use private variables and functions in other modules, you need to
export them. This allows other parts of your application to access and utilize
those variables and functions.
Now go and practice and revise.
extension.
Syntax of Es modules
in non-strict mode.
module.exports = {
x,
calculateSum
};
module.exports.x = x;
module.exports.calculateSum = calculateSum;
Let’s create an
index.js file in our calculate folder.
But why are we doing all this? When you have a large project with hundreds of
files, it’s beneficial to group related files together and create separate modules. In
this case, the calculate folder serves as a new module that encapsulates related
functionality, such as the calculateSum and calculateMultiply functions.
By organizing your code into modules, you improve maintainability, readability,
and scalability, making it easier to manage and navigate your codebase.