Hi, I'm Subham Maity, a software engineer. I also enjoy teaching others how to code through my tutorials. I'm always eager to learn new things and share my knowledge with the community.
⚡ I recently wrote an article on What are Core Modules? and wanted to share it with you all. You can find the article on my website https://codexam.vercel.app/docs/node/node1 [Better View]
⚡ I also have a repository on GitHub where you can find all the code and projects related to this topic. You can find the repository at https://github.com/Subham-Maity/node-js-full-stack-tutorial
❗ For a more in-depth look at this topic, including a detailed table of contents, check out the complete tutorial on my GitHub Repo
If you want to stay updated with my latest projects and articles, you can follow me on:
Core modules are modules that are built into Node.js and come automatically with its installation. These modules include bare minimum functionalities of Node.js and are compiled into its binary distribution. You can load these modules into your program by using the require function
Some examples of core modules in Node.js include http
, fs
, path
, os
, and buffer
among others. These modules provide basic functionalities like creating an HTTP server (http
), handling the file system (fs
), dealing with file paths (path
), providing information about the operating system (os
), and handling binary data (buffer
) .
⭐ What is global modules ?
Explain
Global modules are Node.js packages that are installed on your system rather than your project directory. This means that they can be used anywhere on your local computer.There is no need to import them into your project.
For example, if you install the nodemon
package globally, you can use it anywhere on your computer. You don’t need to import it into your project. You can simply run nodemon
in your terminal to start the server.
console.log("Hello World")
console.log is a global module
⭐ What is Non-global modules ?
In Node.js, a non-global module refers to a module that is not part of the Node.js core and is not installed globally. Instead, it is created locally within your Node.js application. These modules can be a single file or a collection of multiple files/folders. And you need to import them into your project before you can use them.
- Example 1 :
const fs = require('fs')
fs.writeFileSync('hello.txt', 'Hello World')
fs (file system) is a non-global module and we use this module to create a file named hello.txt and write "Hello World" in it.
- Example 2 :
console.log("Our directory is " + __dirname)
//output : Our directory is C:\Users\Subham Maity\Desktop\Node.js\Node.js Tutorial
__dirname is a non-global module and we use this module to get the current directory path.
console.log("Our file name is " + __filename)
//output : Our file name is C:\Users\Subham Maity\Desktop\Node.js\Node.js Tutorial\index.js
__filename is a non-global module and we use this module to get the current file path.
⚡ It's recommended always const abc = require('abc')
should be at the top of the file and not in the middle of the code but if you want to use it in the middle of the code then it will work but it's not recommended.
If you want to import only a specific function from a module, you can use the destructuring syntax. For example, if you want to import the
writeFileSync
function from thefs
module, you can do it like this:
const { writeFileSync } = require('fs')
writeFileSync('hello.txt', 'Hello World')
or
const fs = require('fs').writeFileSync;
fs('hello.txt', 'Hello World')
- here if you want you can change the variable name of the function like this :
const abc = require('fs').writeFileSync;
abc('hello.txt', 'Hello World')