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

2 Comments

@niteshguptav63
niteshguptav63 Nov 17, 2024 at 1:39 PM

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

@niteshguptav63
niteshguptav63 Nov 16, 2024 at 10:56 AM

When will I get my course?

@admin79
admin79 Nov 17, 2024 at 1:29 PM

Now, Your query was resolved.

Frequently Asked Questions About Sciaku Courses & Services

Quick answers to common questions about our courses, quizzes, and learning platform

Didn't find what you're looking for?

help_center Contact Support