Node.js Module System: Common JS

NicestMarimba avatar
NicestMarimba
·
·
Download

Start Quiz

Study Flashcards

18 Questions

What is the purpose of the fs module in Node.js?

To interact with the file system

Which function is used to write data to a file in Node.js?

fs.writeFileSync()

What does JSON stand for?

JavaScript Object Notation

What is the purpose of the JSON.stringify() function?

To convert an object to a string

What does CRUD stand for in the context of a web application?

Create, Read, Update, Delete

What is the purpose of the process.argv array in Node.js?

To store command line arguments

What is the role of the Common JS Module System in Node.js?

To break code into smaller chunks and manage dependencies

How does the 'require' function work in the Common JS Module System?

It imports JavaScript from separate files

What is the purpose of destructuring when using 'require' in Node.js?

To import only specific functions from a module

What is the role of the Process module in Node.js?

To perform tasks immediately before the process exits, and when it exits

What does process.argv represent in Node.js?

An array containing your console information for your executed process

What is the difference between setTimeout() in browsers and setImmediate() in Node.js?

setImmediate() has a higher priority than setTimeout() in Node.js

What is the purpose of the path.normalize method in the Common JS Module System?

To normalize a path by removing instances of '.', turning double slashes into single slashes, and removing a directory when '..' is found

What is the primary function of the Event Loop in Node.js?

To manage the execution of asynchronous operations by continuously running a semi-infinite loop

What does process.nextTick do in Node.js?

Allows you to run JavaScript between the different phases of the event loop

What is a primary advantage of using the Common JS Module System?

It enables cross-platform compatibility by providing modules like path for normalizing paths

What is a primary difference between the fs module and the path module?

The fs module is used for file I/O operations, while the path module is used for manipulating and transforming file paths

What is the purpose of the CLI in Node.js?

To create a user interface for command-line applications

Study Notes

Node.js File System (Fs) Module

  • The Fs module in Node.js stands for "File System" and provides an API for interacting with the file system, modeled on standard POSIX functions.
  • It allows you to work with files by performing various operations like reading, writing, and manipulating.
  • Common Fs module functions include fs.readFileSync() and fs.writeFileSync().

JSON (JavaScript Object Notation)

  • JSON is a lightweight data-interchange format that is plain text written in JavaScript object notation.
  • It is used to send data between computers and is language independent.
  • JSON has two main functions:
    • JSON.stringify(): Converts an object or array of objects to a JSON string, accepting any type of data and returning a string in JSON format.
    • JSON.parse(): Converts a string in JSON format to an object, accepting only strings in JSON format and throwing an error when trying to parse a string not in JSON format.

CLI (Command Line Interface) Example

  • We can create a simple CRUD (Create, Read, Update, Delete) operation app that uses the file system to save data.
  • We will use a JSON file as a database to store data about employees (name, salary).

Common JS Module System

  • Node.js used the Common JS module system to break code into smaller chunks, which is still used today.
  • The module system creates the ability to export and import JavaScript from separate files.
  • require() is used to import modules, and destructuring is often used when only requiring one function from an otherwise large library.

Process and Timers

  • The process module contains the ability to perform tasks immediately before the process exits, and when it exits.
  • process.argv is an array containing your console information for your executed process.
  • The Node.js timers API is similar, but not identical to the browser API.
  • setImmediate() and clearImmediate() are used in Node.js.

Path Module

  • The path module is used to normalize paths to work across platforms.
  • path.resolve() enables you to get the absolute path from a relative path.
  • path.normalize() normalizes any path by removing instances of . and .., turning double slashes into single slashes, and removing a directory when .. is found.
  • path.join() is used to concatenate strings to create a path that works across operating systems.

Event Loop

  • Node.js is a single-threaded, non-blocking, event-driven JavaScript runtime environment.
  • Nearly every Node.js feature is considered to be asynchronous (non-blocking).
  • The Event Loop is a process that runs anytime you have asynchronous code, allowing your application to continue running while requests are being waited for.
  • The Event Loop is a continuously running, semi-infinite loop that runs for as long as there is a pending asynchronous operation.

Learn about how Node.js utilized the Common JS module system to break code into smaller chunks and export/import JavaScript from separate files. Explore the concepts of exporting and importing modules, using 'require' to include modules, and best practices like omitting file extensions.

Make Your Own Quizzes and Flashcards

Convert your notes into interactive study material.

Get started for free

More Quizzes Like This

Use Quizgecko on...
Browser
Browser