Modules

Dear Sciaku Learner you are not logged in or not enrolled in this course.

Please Click on login or enroll now button.

If you have any query feel free to chat us!

Happy Coding! Happy Learning!

Lecture 230:- Modules

In Node.js, modules are reusable pieces of code that can be imported and used in other files or modules. They provide an organized way to structure code and can help to improve the maintainability of a project.

Node.js uses a CommonJS module system, which allows modules to be defined in separate files and exported using the module.exports or exports object. These objects contain the functions, objects, or values that are available to other modules that import them.

To use a module in a Node.js application, we can use the require() function to import the module into our code. The require() function takes a module path as an argument, which can be either a relative or absolute path to the module file. Once the module is imported, we can access its exported functions, objects, or values using dot notation.

Here is an example of how to define and use a simple module in Node.js:

cssCopy code

// math.js function add(a, b) {  return a + b; } function subtract(a, b) {  return a - b; } module.exports = {  add: add,  subtract: subtract };

javascriptCopy code

// app.js const math = require('./math'); console.log(math.add(2, 3));      // Output: 5 console.log(math.subtract(5, 2)); // Output: 3

In this example, we define a math module that exports the add and subtract functions. We then import the math module into our app.js file using the require() function and access its exported functions using dot notation.

27. Nodejs - The Beginning

Comments: 2

profile
@niteshguptav63
17-Nov-2024, 01:39 PM

I am not able to access videos from second class and further. I have already completed first class

profile
@niteshguptav63
16-Nov-2024, 10:56 AM

When will I get my course?

profile
@admin79
17-Nov-2024, 01:29 PM

Now, Your query was resolved.

Frequently Asked Questions (FAQs)

How do I register on Sciaku.com?
How can I enroll in a course on Sciaku.com?
Are there free courses available on Sciaku.com?
How do I purchase a paid course on Sciaku.com?
What payment methods are accepted on Sciaku.com?
How will I access the course content after purchasing a course?
How long do I have access to a purchased course on Sciaku.com?
How do I contact the admin for assistance or support?
Can I get a refund for a course I've purchased?
How does the admin grant access to a course after payment?