Navigate
JavaScript Tutorial for Beginners
The Ultimate CSS Tutorial for Beginners
Data Structure Tutorial for Beginners
ReactJS Tutorial for Beginners
Java Tutorial for Beginners
Node.js Tutorial for Beginners
SQL Tutorial for Beginners
Working with Modules
Content
- What are Modules?
- Built-in-Modules
- Creating a Custom Module
- Activity
- Quiz
- Updated on 10/09/2024
- 450 Views
What are Modules?
Modules are reusable pieces of code that can be included in your Node.js applications. They help you organize your code into manageable sections.
Built-in-Modules
Node.js comes with several built-in modules, such as http, fs (file system), and path.
Creating a Custom Module
Create a new file myModule.js: Use the module in another file:
Activity
Create your own module that exports a function to calculate the area of a rectangle. Import and use this module in another file.
Quiz
How do you include a built-in module in Node.js?
- a) import moduleName
- b) require('moduleName')
- c) include('moduleName')
- d) module('moduleName')
What is the purpose of module.exports in Node.js?
- a) To import modules
- b) To define local variables
- c) To export functions and variables from a module
- d) To manage dependencies
Which built-in Node.js module is used to interact with the file system?
- a) http
- b) fs
- c) path
- d) os
How do you import a custom module in Node.js?
- a) import './myModule';
- b) require('./myModule');
- c) include('./myModule');
- d) load('./myModule');
What will be the output of the following code if myModule.js exports a function greet that returns 'Hello, World!'?
- a) Hello, World!
- b) Hello, Node.js!
- c) Hello!
- d) World!