Core Modules & Global Object in Node

What Are Modules and Their Types?

Core Modules

  1. Core modules are already available in Node.js.
  2. You do not need to install them.
const fs = require("fs");
const os = require("os"); // import os module

fs.writeFileSync("dummy.txt", "trying with modules");
// Create a file with name and write content in it

console.log(os.platform()); // Print the platform of the OS
console.log(os.hostname()); // Print the hostname of the OS
console.log(os.cpus()); // Print the CPU information of the OS


Third-party Modules

You can install them from third party.


Custom Modules

You create them by yourself.


What Are Core Modules?

  1. Core modules are modules that are already included in Node.js.
  2. No need to install them separately. Just use require() to import them.


What Are Global Objects?

  1. Global object is an object that is available in all modules by default.
  2. You do not need to import or require it to use it.


You can import console forcefully:

const { log } = require("console"); // import console module

log("custom log");


Difference Between console.log in JS and Node.js

The difference is:

  1. In JavaScriptconsole.log prints on the browser console.
  2. In Node.jsconsole.log prints on the terminal.