Open In App

How to Get Information About a File using Node.js ?

Last Updated : 02 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Node.js is an open-source and cross-platform runtime environment built on Chrome’s V8 JavaScript engine for executing JavaScript code outside of a browser. You need to recollect that NodeJS isn’t a framework, and it’s not a programming language. In this article, we will discuss how to get information about a file using Node.js

Approach

We will use the fs module of Node.js to extract information about files. The fs module is an inbuilt module. We will use the fs.stat() module of the fs module to get all the information related to the file. If to get information about the uploaded files then we can handle them using npm packages like multer which handles all different types of files. Let us go through step by step. First, create a file in the current working directory whose information you want to view.

Node fs.stat() method 

The fs.stat() method is used to return information about the given file or directory. It returns an fs.Stat object which has several properties and methods to get details about the file or directory.

Syntax:

fs.stat( path, options, callback )

Attributes:

PropertyDescriptionType
devThe id of the device containing the file.bigInt or number
modeBit-field description of the file type and mode of the given file.bigInt or number
nlinkThe number of hard links for the file.bigInt or number
uidThe User Id of the file owner.bigInt or number
gidThe Group Id of the file owner.bigInt or number
rdevDevice id of the file if it is a special file.bigInt or number
blksizeThe block size for a file system Input/Output.bigInt or number
inoFile inode number. It contains the basic information about the file.bigInt or number
sizeThe total size of the file in bytes.bigInt or number
blocksThe number of blocks allocated to the given file.bigInt or number
atimeMsThe timestamp representing the file’s last accessed time.bigInt or number
mtimeMsThe timestamp representing the file’s last modified time.bigInt or number
ctimeMsThe timestamp representing the file last changed time, when the inode was changed.bigInt or number
birthtimeMsThe timestamp representing when the file was created.bigInt or number
aTimeThe Date object representing the file’s last access time.Date
mTimeThe Date object representing the file’s last modified time.Date
cTimeThe Date object representing the file’s last changed time.Date
birthtimeThe Date object representing when the file was created.Date

Steps to Get Information About a File using Node.js

Step 1: Create an "app.js" file and initialize your project with npm.

npm init

Step 2: Create an info.txt file in the project folder.

Project structure:

Project/File Structure

Example: This example demonstrates getting file information in with Node js using fs.stat() method.

Node
// app.js

//Importing fs module 
const fs = require("fs");

//stat methods takes path and a callback function as input
fs.stat("./info.txt", function(err, stats){

    //Checking for errors
   if(err){
       console.log(err)
   }
   else{
    //Logging the stats Object
   console.log(stats)
   }
});

Run app.js file using below command:

node app.js

Output:

Output

So by using the file system of nodeJS you can get all the required information about any file in the local file system.


Next Article

Similar Reads