Internal modules in Node.js refer to built-in modules that are available within the Node.js environment. These modules are part of the Node.js core and provide a range of functionalities and utilities that can be used in your application without the need to install any external packages.
There are several internal modules available in Node.js, including but not limited to:
To use these modules in your Node.js application, you need to import them using the require()
function. For example, to use the fs
module to read a file in your application, you would write:
const fs = require('fs');
fs.readFile('file.txt', (err, data) => {
if (err) throw err;
console.log(data);
});
Starting from Node.js version 13, you can also use ES6 module syntax to import internal modules.
import fs from 'fs';
With Node.js V18, you can be explicit about importing core, internal modules like so:
import fs from 'node:fs'
Using the node:
prefix doesn’t change the behavior of the import, it’s just a way to be explicit about this module being a core module and not a 3rd party one.
In summary, internal modules in Node.js are built-in modules that provide a range of functionalities and utilities. You can import them using either the require()
function or ES6 module syntax, depending on your Node.js version and the type of module you want to import.