Mastering NodeJS: A Comprehensive Guide to Node Js REPL|| Lesson - 1

Mastering NodeJS: A Comprehensive Guide to Node Js REPL|| Lesson - 1

Table of contents

In the ever-evolving landscape of web development, Node.js stands out as a dynamic and robust platform that has redefined the way JavaScript is used. As the lines between front-end and back-end development blur, Node.js emerges as a key player, enabling developers to wield the power of JavaScript on the server side.

This blog aims to be your compass through the expansive world of Node.js, breaking down its intricacies into digestible chapters. Whether you're a seasoned developer seeking to enhance your skills or a newcomer eager to explore the vast capabilities of Node.js, this comprehensive guide will walk you through its fundamental concepts, installation procedures, and practical applications.

Chapter 1: What is Node.js?

In the vast landscape of web development technologies, Node.js emerges as a game-changer, introducing the capability to run JavaScript on the server side. Unlike traditional JavaScript, which primarily operates within web browsers, Node.js extends the reach of this versatile scripting language to server environments, enabling developers to create efficient and scalable network applications.

Understanding the Basics:

1.1 JavaScript Beyond the Browser:

Traditionally, JavaScript has been synonymous with client-side scripting, enhancing user interfaces and interactivity within web browsers. However, as the demand for real-time, high-performance applications grew, developers sought ways to leverage JavaScript on the server side. Node.js was born as a response to this need.

1.2 Server-Side JavaScript:

Node.js is built on the V8 JavaScript runtime, originally developed for Google Chrome. It executes JavaScript code outside of the browser, allowing developers to utilize the language for server-side scripting. This breakthrough opens the door to building entire web applications using a single language—JavaScript—from front-end to back-end.

Key Features of Node.js:

1.3 Asynchronous and Event-Driven:

One of Node.js's defining features is its asynchronous, non-blocking I/O model. This design choice enhances the efficiency of handling concurrent operations, making it well-suited for applications requiring high levels of responsiveness, such as real-time chat applications and online gaming platforms.

1.4 Single-Threaded, Event Loop:

Node.js employs a single-threaded event loop, making it highly efficient in managing concurrent operations. This architecture allows Node.js to handle a large number of simultaneous connections without the need for multithreading, making it a lightweight yet powerful choice for server-side development.

Use Cases and Applications:

1.5 Widening Application Horizons:

Node.js has found widespread adoption in various domains, including e-commerce, social media, streaming services, and more. Its ability to handle a large number of concurrent connections, coupled with its speed and scalability, positions it as a go-to technology for modern web development.

1.6 Real-Time Capabilities:

The event-driven architecture of Node.js makes it particularly suitable for real-time applications. Whether it's live chat functionality, collaborative document editing, or real-time analytics, Node.js empowers developers to create seamless, responsive experiences for users.

The Journey Ahead:

1.7 Navigating the Guide:

As we embark on this exploration of Node.js, each chapter will unravel specific aspects, from installation procedures to practical hands-on applications. By the end of this guide, you'll not only understand what Node.js is but also appreciate its transformative impact on modern web development.

Join us on this journey through the heart of Node.js as we demystify its core concepts, equipping you with the knowledge and skills to harness its full potential. Let's dive into Chapter 2, where we'll explore the installation links to kickstart your Node.js journey.

Chapter 2: Installation Links

To get started with Node.js, you'll need to install it on your machine. Visit the official Node.js website (https://nodejs.org/) to find installation links for various operating systems. Follow the instructions provided to set up Node.js on your computer.

For Mac Users : https://nodejs.org/en

For Windows Users : https://nodejs.org/en/download

Windows user can also refer this video : https://www.youtube.com/watch?v=EIzdQxMXcrc

Chapter 3: Node REPL

In this chapter, we'll dive into the Node.js REPL (Read-Eval-Print Loop), an interactive environment that allows you to experiment with JavaScript code in a quick and iterative manner.

3.1 Understanding REPL:

Node REPL is a command-line tool that provides a way to interact with Node.js in real-time. It allows you to enter JavaScript code, which is then evaluated, and the result is immediately displayed. This instant feedback makes it an excellent tool for testing code snippets, debugging, and learning JavaScript concepts.

3.2 Accessing Node REPL:

To access the Node.js REPL, open your terminal or command prompt and type node. This will launch the REPL, and you'll see the > prompt, indicating that Node.js is ready to accept your JavaScript commands.

$ node
>

3.3 Basic Operations:

Let's start with some basic operations in the Node REPL:

// Arithmetic operations
> 5 + 3
8

// Variable assignment
> let message = "Hello, Node REPL!";
undefined

// Printing variables
> console.log(message);
Hello, Node REPL!
undefined

Note that the undefined after variable assignment is the result returned by the REPL. The last result of any expression is automatically stored in the _ (underscore) variable.

3.4 Multiline Commands:

The Node REPL supports multiline commands. If you want to write a function or a multiline statement, use the ... prompt:

// Multiline function
> function greet(name) {
...   return "Hello, " + name + "!";
... }
undefined

> greet("John");
'Hello, John!'

3.5 Special Commands:

The REPL provides special commands prefixed with a dot (.). Here are a few useful ones:

  • .help: Display the list of special commands.

  • .break: Exit from a multiline expression.

  • .clear: Reset the REPL context.

  • .save and .load: Save and load the current REPL session.

3.6 Exiting the REPL:

To exit the Node REPL, you can use the .exit command or press Ctrl + C twice.

> .exit
$

Conclusion:

Node REPL is an invaluable tool for exploring and testing JavaScript code interactively. It allows you to quickly prototype ideas, test snippets, and gain a deeper understanding of how JavaScript behaves in a live environment. As we continue our Node.js journey, this interactive nature will prove to be a valuable asset in learning and developing with Node.js. In the next chapter, we'll explore the installation links to set up Node.js on your machine.

Chapter 4: Node Files

In this chapter, we'll explore how Node.js handles files. Understanding file operations is fundamental for building applications that read, write, and manipulate data on the server.

4.1 Reading from Files:

Node.js provides the fs (File System) module for file-related operations. Let's start by reading the contents of a file asynchronously using fs.readFile:

const fs = require('fs');

// Reading a file asynchronously
fs.readFile('example.txt', 'utf8', (err, data) => {
  if (err) {
    console.error('Error reading the file:', err);
    return;
  }
  console.log('File contents:', data);
});

In this example, example.txt is the name of the file to be read, and 'utf8' specifies the encoding. The callback function receives any errors that occur during the file read and the file data.

4.2 Writing to Files:

To write content to a file, you can use fs.writeFile. If the file doesn't exist, it will be created; if it does exist, its content will be replaced:

const fs = require('fs');

const content = 'Hello, Node Files!';

// Writing to a file asynchronously
fs.writeFile('output.txt', content, 'utf8', (err) => {
  if (err) {
    console.error('Error writing to the file:', err);
    return;
  }
  console.log('File written successfully!');
});

In this example, we're writing the string Hello, Node Files! to a file named output.txt.

4.3 Checking if a File Exists:

Before performing file operations, it's often useful to check if a file exists. You can use fs.existsSync:

const fs = require('fs');

const filename = 'example.txt';

if (fs.existsSync(filename)) {
  console.log(`${filename} exists.`);
} else {
  console.log(`${filename} does not exist.`);
}

4.4 Working with File Paths:

Understanding and constructing file paths is crucial. Node.js provides the path module for working with file and directory paths. Here's a simple example:

const path = require('path');

const filePath = path.join(__dirname, 'files', 'example.txt');
console.log('Full file path:', filePath);

In this example, __dirname represents the current directory, and path.join combines the directory with the file name to create a full path.

Conclusion:

Understanding file operations in Node.js is essential for building practical applications. Whether you're reading configuration files, handling user uploads, or managing server logs, mastering file handling with Node.js opens up a world of possibilities. In the next chapter, we'll explore the process object in Node.js and its significance in managing the Node.js runtime environment.

Chapter 5: Process in Node

In this chapter, we'll explore the process object in Node.js, a global object that provides information about the Node.js process running the script. Understanding the process object is crucial for tasks such as accessing command line arguments, managing environment variables, and interacting with the Node.js runtime.

5.1 Accessing Command Line Arguments:

Node.js allows you to pass command line arguments when executing a script. The process.argv array contains the command line arguments. Let's create a simple script that prints the command line arguments:

// commandLineArgs.js

// Accessing command line arguments
const args = process.argv.slice(2);
console.log('Command line arguments:', args);

Run this script from the terminal:

$ node commandLineArgs.js arg1 arg2 arg3

The output will be:

Command line arguments: [ 'arg1', 'arg2', 'arg3' ]

5.2 Environment Variables:

Environment variables are key-value pairs that can be passed to a Node.js process. The process.env object contains the environment variables. Let's create a script that accesses environment variables:

// environmentVariables.js

// Accessing environment variables
const environmentVar = process.env.NODE_ENV || 'development';
console.log('Environment:', environmentVar);

You can set an environment variable before running the script:

$ NODE_ENV=production node environmentVariables.js

The output will be:

Environment: production

5.3 Exiting the Process:

You can exit a Node.js process programmatically using process.exit(code). The exit code is optional and defaults to 0 (success). Here's an example:

// exitProcess.js

// Exiting the Node.js process
console.log('Exiting the process...');
process.exit(1); // Exit with code 1 (indicating failure)

When you run this script:

$ node exitProcess.js

The process will exit with code 1.

5.4 Event Listeners for Process:

The process object emits events that you can listen to. One common event is the exit event, which is emitted when the Node.js process is about to exit. Let's create a script that listens for the exit event:

// exitEvent.js

// Event listener for process exit
process.on('exit', (code) => {
  console.log(`Process is about to exit with code: ${code}`);
});

console.log('Script execution completed.');

When you run this script:

$ node exitEvent.js

The output will be:

Script execution completed.
Process is about to exit with code: 0

Conclusion:

The process object in Node.js is a powerful tool for interacting with the runtime environment. Whether you're handling command line arguments, accessing environment variables, or managing the exit process, understanding the capabilities of the process object is essential. In the next chapter, we'll explore the concept of exporting functionality from one module to another using the module.exports statement.

Chapter 6: Export in Files

In this chapter, we'll explore the concept of exporting functionality from one module to another in Node.js using the module.exports statement. This mechanism allows you to create reusable modules and organize your code effectively.

6.1 Creating a Module:

Let's start by creating a simple module that defines a function. Create a file named mathOperations.js:

// mathOperations.js

// Exporting a function
const add = (a, b) => a + b;

module.exports = add;

In this example, we've defined a function add and exported it using module.exports.

6.2 Importing the Module:

Now, let's create another file, say main.js, where we import the module and use the exported function:

// main.js

// Importing the module
const addFunction = require('./mathOperations');

// Using the imported function
const result = addFunction(5, 3);
console.log('Result:', result);

In this script, we use require('./mathOperations') to import the mathOperations module. The exported function (add) is now available as addFunction.

6.3 Exporting Multiple Functions:

You can export multiple functions or variables from a module. Let's extend our mathOperations.js module to export both addition and subtraction functions:

// mathOperations.js

// Exporting multiple functions
const add = (a, b) => a + b;
const subtract = (a, b) => a - b;

module.exports = {
  add,
  subtract,
};

Now, in main.js, we can import both functions:

// main.js

// Importing multiple functions from the module
const { add, subtract } = require('./mathOperations');

// Using the imported functions
const resultAdd = add(5, 3);
const resultSubtract = subtract(8, 3);

console.log('Result of addition:', resultAdd);
console.log('Result of subtraction:', resultSubtract);

6.4 Exporting Variables:

Besides functions, you can export variables as well. Let's modify our mathOperations.js module to export a constant:

// mathOperations.js

// Exporting a variable
const PI = 3.14159;

module.exports = {
  add,
  subtract,
  PI,
};

Now, in main.js, we can import and use the exported variable:

// main.js

// Importing functions and a variable from the module
const { add, subtract, PI } = require('./mathOperations');

// Using the imported functions and variable
const resultAdd = add(5, 3);
const resultSubtract = subtract(8, 3);

console.log('Result of addition:', resultAdd);
console.log('Result of subtraction:', resultSubtract);
console.log('Value of PI:', PI);

Conclusion:

Exporting functionality in Node.js using module.exports is a fundamental concept for building modular and maintainable applications. Whether you're exporting functions or variables, this mechanism allows you to encapsulate logic and reuse it across different parts of your codebase. In the next chapter, we'll extend this concept to exporting modules from directories.

Chapter 7: Export in Directories

In this chapter, we'll explore the concept of exporting modules from directories in Node.js. This allows you to organize your code into cohesive modules and simplify the import process when dealing with multiple related functionalities.

7.1 Creating a Module Directory:

Let's start by creating a directory named utils that will contain multiple modules. Inside the utils directory, create two files: mathOperations.js and stringOperations.js.

mathOperations.js:

// utils/mathOperations.js

// Exporting math functions
const add = (a, b) => a + b;
const subtract = (a, b) => a - b;

module.exports = {
  add,
  subtract,
};

stringOperations.js:

// utils/stringOperations.js

// Exporting string functions
const capitalize = (str) => str.toUpperCase();
const reverse = (str) => str.split('').reverse().join('');

module.exports = {
  capitalize,
  reverse,
};

7.2 Importing Modules from a Directory:

Now, let's create a main file, say main.js, where we import modules from the utils directory:

// main.js

// Importing modules from the 'utils' directory
const mathModule = require('./utils/mathOperations');
const stringModule = require('./utils/stringOperations');

// Using functions from the imported modules
const resultAdd = mathModule.add(5, 3);
const resultSubtract = mathModule.subtract(8, 3);

const capitalizedString = stringModule.capitalize('node.js');
const reversedString = stringModule.reverse('hello');

console.log('Result of addition:', resultAdd);
console.log('Result of subtraction:', resultSubtract);
console.log('Capitalized string:', capitalizedString);
console.log('Reversed string:', reversedString);

In this script, we use require('./utils/mathOperations') and require('./utils/stringOperations') to import modules from the utils directory. The exported functions from each module are then accessible using the respective module names (mathModule and stringModule).

7.3 Using an Index File:

To simplify the import process further, you can create an index.js file inside the utils directory. This file acts as a central export point for all modules within the directory.

utils/index.js:

// utils/index.js

// Exporting all modules
const mathModule = require('./mathOperations');
const stringModule = require('./stringOperations');

module.exports = {
  mathModule,
  stringModule,
};

Now, in main.js, you can import modules directly from the utils directory:

// main.js

// Importing modules from the 'utils' directory using index.js
const { mathModule, stringModule } = require('./utils');

// Using functions from the imported modules
const resultAdd = mathModule.add(5, 3);
const resultSubtract = mathModule.subtract(8, 3);

const capitalizedString = stringModule.capitalize('node.js');
const reversedString = stringModule.reverse('hello');

console.log('Result of addition:', resultAdd);
console.log('Result of subtraction:', resultSubtract);
console.log('Capitalized string:', capitalizedString);
console.log('Reversed string:', reversedString);

This approach enhances code organization and simplifies the import syntax.

Conclusion:

Exporting modules from directories in Node.js provides a clean and organized way to structure your code. By encapsulating related functionalities in separate modules and using an index file, you can streamline the import process and create a modular architecture for your Node.js applications. In the next chapter, we'll delve into the world of npm (Node Package Manager) and explore its role in managing external packages and dependencies.

Chapter 8: What is npm

In this chapter, we'll explore the fundamental concept of npm (Node Package Manager), an essential tool in the Node.js ecosystem. npm is used for package management, facilitating the installation, sharing, and version control of JavaScript libraries and tools. Let's dive into npm and understand its key features.

8.1 Installing Node.js:

Before we explore npm, ensure that you have Node.js installed on your machine. You can download it from the official website: Node.js Downloads.

Once Node.js is installed, npm is automatically installed alongside it.

8.2 Checking npm Version:

To check the installed version of npm, open your terminal or command prompt and run the following command:

npm -v

This will display the version number of npm.

8.3 Initializing a Node.js Project:

To use npm for package management in your project, you first need to initialize a package.json file. Navigate to your project directory in the terminal and run:

npm init

Follow the prompts to provide information about your project. This will generate a package.json file with your project's metadata.

8.4 Understanding package.json:

The package.json file is a critical component of Node.js projects. It includes information about your project, its dependencies, scripts, and more. Here's a simplified example:

{
  "name": "my-node-project",
  "version": "1.0.0",
  "description": "A simple Node.js project",
  "main": "index.js",
  "scripts": {
    "start": "node index.js"
  },
  "dependencies": {
    "express": "^4.17.1"
  }
}
  • name: The name of your project.

  • version: The version number of your project.

  • description: A brief description of your project.

  • main: The entry point script of your application.

  • scripts: Custom scripts that can be executed using npm.

  • dependencies: External packages that your project depends on.

8.5 Installing Packages:

npm makes it easy to install packages and manage dependencies. For example, to install the Express framework, you can run:

npm install express

This command installs the latest version of Express and adds it to your project's dependencies in package.json.

8.6 Understanding Semantic Versioning:

npm uses semantic versioning (SemVer) to manage package versions. A version number consists of three parts: MAJOR.MINOR.PATCH. The ^ symbol in the dependencies section of package.json indicates that your project can use any compatible version up to the next major release.

8.7 Uninstalling Packages:

To uninstall a package, you can use the uninstall or remove command:

npm uninstall express

This removes the package from your dependencies in package.json.

Conclusion:

npm is a powerful tool that simplifies the process of managing dependencies in Node.js projects. It enables developers to easily install, update, and remove packages, ensuring a smooth development experience. In the next chapter, we'll dive into the details of installing packages and explore the node_modules directory.

Chapter 9: Installing Packages

In this chapter, we'll delve into the practical aspects of installing packages using npm in a Node.js project. Installing packages is a crucial step in leveraging external libraries and tools to enhance your project's functionality. Let's explore various scenarios of installing packages and managing dependencies.

9.1 Installing Specific Versions:

npm allows you to install packages at specific versions. You can specify the version number in the following ways:

# Install the exact version
npm install package-name@1.2.3

# Install the latest patch version in the 1.2.x range
npm install package-name@1.2

# Install the latest minor version in the 1.x range
npm install package-name@1

For example, to install the lodash library at version 4.17.21, you can run:

npm install lodash@4.17.21

9.2 Installing Packages Globally:

Some packages provide command-line tools that you may want to use across different projects. To install a package globally, use the -g flag:

npm install -g package-name

For instance, to install the nodemon package globally, which is often used for automatically restarting Node.js applications during development, you can run:

npm install -g nodemon

9.3 Installing Packages as Development Dependencies:

Dependencies in your project can be categorized into two types: runtime dependencies and development dependencies. Development dependencies are only needed during development, not when the project is in production. Use the --save-dev flag to install a package as a development dependency:

npm install package-name --save-dev

For example, to install the Jest testing framework as a development dependency, you can run:

npm install jest --save-dev

9.4 Installing Packages from package.json:

If you have a package.json file with listed dependencies, you can install all of them by running:

npm install

This command reads the dependencies and devDependencies sections of your package.json and installs all the listed packages.

9.5 Updating Packages:

To update packages to their latest versions based on the version ranges specified in your package.json, you can use:

npm update

This command updates packages to the latest versions within the specified version constraints.

9.6 Uninstalling Packages:

To remove a package from your project, you can use the uninstall or remove command:

npm uninstall package-name

For instance, to uninstall the Jest testing framework, you can run:

npm uninstall jest

Conclusion:

Understanding how to install, update, and uninstall packages is fundamental for managing dependencies in a Node.js project. npm simplifies the process of integrating external libraries and tools, enabling developers to build robust and feature-rich applications. In the next chapter, we'll explore the heart of Node.js projects—the package.json file—and understand its role in project configuration and dependency management.

Chapter 10: Everything about package.json

In this chapter, we'll explore the package.json file, a crucial component of Node.js projects. This file serves as the configuration file for your project, capturing metadata, scripts, and dependencies. Understanding the package.json structure is essential for managing and sharing Node.js applications.

10.1 Initializing package.json:

To create a package.json file for your project, run the following command in your project directory:

npm init

Follow the prompts to provide information about your project, such as the project name, version, entry point, test command, repository, keywords, author, and license. You can also skip the prompts and use the default values by adding the -y flag:

npm init -y

10.2 package.json Structure:

A typical package.json file has the following structure:

{
  "name": "my-node-project",
  "version": "1.0.0",
  "description": "A simple Node.js project",
  "main": "index.js",
  "scripts": {
    "start": "node index.js",
    "test": "jest"
  },
  "keywords": ["node", "javascript"],
  "author": "Your Name",
  "license": "MIT",
  "dependencies": {
    "express": "^4.17.1",
    "lodash": "^4.17.21"
  },
  "devDependencies": {
    "jest": "^27.0.6"
  }
}
  • name: The name of your project.

  • version: The version number of your project.

  • description: A brief description of your project.

  • main: The entry point script of your application.

  • scripts: Custom scripts that can be executed using npm.

  • keywords: An array of keywords describing your project.

  • author: The author of the project.

  • license: The license under which the project is distributed.

  • dependencies: External packages required for the project to run.

  • devDependencies: External packages required for development and testing.

10.3 Scripts in package.json:

The scripts section allows you to define custom commands that can be executed with npm. Common scripts include:

  • start: The script to run your application.

  • test: The script to run tests.

  • build: The script to build your project.

  • lint: The script to run code linters.

For example, the "start" script might execute the main file of your application:

"scripts": {
  "start": "node index.js"
}

To run this script, you can use:

npm start

10.4 Adding and Removing Dependencies:

When you install a package using npm install package-name, npm automatically updates the dependencies section in your package.json. Similarly, when you uninstall a package using npm uninstall package-name, npm updates the dependencies section accordingly.

10.5 Lock File: package-lock.json

When you install packages, npm generates a package-lock.json file. This file locks down the version numbers of the installed packages, ensuring consistent installations across different environments.

Conclusion:

The package.json file is the backbone of Node.js projects, capturing essential information about the project, its dependencies, and configuration details. Understanding how to initialize, customize, and manage the package.json file is crucial for effective Node.js development. In the next chapter, we'll explore the difference between local and global package installations using npm.

Chapter 11: Local vs Global Installation

In this chapter, we'll explore the differences between local and global installations of npm packages. Understanding when to use each type of installation is crucial for effective Node.js development.

11.1 Local Installation:

Local installations are the default and are typically used for project-specific dependencies. When you run:

npm install package-name

The package is installed locally within the node_modules directory of your project. This means that the package is available only for that specific project.

Example:

# Install lodash locally
npm install lodash

This installs lodash within the node_modules directory of the current project.

// package.json
{
  "dependencies": {
    "lodash": "^4.17.21"
  }
}

You can then use lodash in your project code:

const _ = require('lodash');

11.2 Global Installation:

Global installations are used for tools and utilities that you want to use across multiple projects. When you run:

npm install -g package-name

The package is installed globally, making it accessible from any directory on your machine.

Example:

# Install nodemon globally
npm install -g nodemon

This installs nodemon globally, allowing you to use it as a command-line tool for automatically restarting Node.js applications during development.

11.3 When to Use Local Installation:

  • Project-specific Dependencies: Use local installations for dependencies that are specific to a particular project. This ensures that each project has its own set of dependencies and versions.

  • Dependency Versioning: Local installations allow you to specify and control the versions of dependencies used in a specific project. This ensures consistency across different environments.

  • Isolation: Local installations provide project isolation. Each project has its own node_modules directory, preventing conflicts between different projects.

11.4 When to Use Global Installation:

  • Command-Line Tools: Global installations are suitable for command-line tools and utilities that you want to use across different projects, such as development servers (nodemon), build tools (webpack), or linters (eslint).

  • Utilities with Global Commands: Some packages provide global commands that you can run from the terminal. Global installations make these commands accessible globally.

  • Tools Used in Multiple Projects: If you find yourself using a tool frequently across various projects, a global installation can save disk space and make it easier to manage.

11.5 Managing Global Packages:

To list globally installed packages, you can use:

npm list -g --depth=0

To uninstall a globally installed package, you can use:

npm uninstall -g package-name

Conclusion:

Understanding the distinction between local and global installations is crucial for efficient Node.js development. Local installations provide project-specific dependencies and version control, while global installations are suitable for command-line tools and utilities used across different projects. By choosing the right type of installation, you can manage dependencies effectively and streamline your development workflow. In the next chapter, we'll explore the process of importing modules in Node.js projects.

Chapter 12: Importing Modules

In this chapter, we'll explore the process of importing modules in Node.js projects. Node.js follows the CommonJS module system, which allows you to break your code into modular pieces, making it more maintainable and reusable. Let's dive into the details of importing modules.

12.1 Creating a Module:

First, let's create a simple module to work with. Create a file named math.js with the following content:

// math.js

const add = (a, b) => a + b;
const subtract = (a, b) => a - b;

module.exports = {
  add,
  subtract,
};

In this module, we have two functions, add and subtract, and we're exporting them using module.exports.

12.2 Importing a Module:

Now, let's create another file, say main.js, where we import and use the math module:

// main.js

// Importing the math module
const mathModule = require('./math');

// Using functions from the imported module
const resultAdd = mathModule.add(5, 3);
const resultSubtract = mathModule.subtract(8, 3);

console.log('Result of addition:', resultAdd);
console.log('Result of subtraction:', resultSubtract);

In this script, we use require('./math') to import the math module. The exported functions (add and subtract) are now available through the mathModule object.

12.3 Importing Specific Functions:

If you are interested in importing only specific functions from a module, you can do so by destructuring the imported object. Let's modify main.js to import only the add function:

// main.js

// Importing only the add function from the math module
const { add } = require('./math');

// Using the imported function
const resultAdd = add(5, 3);

console.log('Result of addition:', resultAdd);

12.4 Core Modules:

Node.js comes with a set of built-in or core modules that you can use without installing additional packages. For example, the fs (File System) module provides functionality to interact with the file system. You can import core modules using the same require syntax:

// Using the fs core module
const fs = require('fs');

12.5 Third-Party Modules:

In addition to core modules, you can use third-party modules by installing them using npm. Once installed, you can import and use them in your project:

npm install lodash
// Using the lodash module
const _ = require('lodash');

12.6 ES6 Import Syntax (Experimental):

Starting from Node.js version 13.2.0, you can use the ES6 import syntax for module importing. However, keep in mind that this feature is still experimental and may require additional configuration.

// ES6 import syntax (experimental)
import { add, subtract } from './math';

const resultAdd = add(5, 3);
const resultSubtract = subtract(8, 3);

console.log('Result of addition:', resultAdd);
console.log('Result of subtraction:', resultSubtract);

To use the ES6 import syntax, ensure your project is configured appropriately, and you are running a version of Node.js that supports this feature.

Conclusion:

Importing modules is a fundamental aspect of Node.js development, allowing you to structure your code, promote reusability, and manage dependencies effectively. Whether you're working with your own modules, core modules, or third-party modules, mastering the module system is key to building scalable and maintainable Node.js applications. In the next chapter, we'll explore the concept of the Node Package Manager (npm) and understand its role in managing external packages and dependencies.

Make Sure to follow Mayank Aggarwal on Hashnode for Next Part.

Did you find this article valuable?

Support Mayank Aggarwal by becoming a sponsor. Any amount is appreciated!