Open In App

How to get information about a file using Node.js ?

Improve
Improve
Like Article
Like
Save
Share
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

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.

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

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

Now before looking over the output let us discuss the attributes of the Stats Object:

  • dev: The id of the device containing the file. (bigInt or number)
  • mode: Bit-field description of the file type and mode of the given file. (bigInt or number)
  • nlink: The number of hard links for the file. (bigInt or number)
  • uid: The User Id of the file owner. (bigInt or number)
  • gid: The Group Id of the file owner. (bigInt or number)
  • rdev: Device id of the file if it is a special file. (bigInt or number)
  • blksize: The block size for a file system Input/Output. (bigInt or number)
  • ino: File inode number. It contains the basic information about the file. (bigInt or number)
  • size: The total size of the file in bytes. (bigInt or number)
  • blocks: The number of blocks allocated to the given file. (bigInt or number)
  • atimeMs: The timestamp representing the file’s last accessed time. (bigInt or number)
  • mtimeMs: The timestamp representing the file’s last modified time. (bigInt or number)
  • ctimeMs: The timestamp representing the file last changed time, when the inode was changed. (bigInt or number)
  • birthtimeMs: The timestamp representing when the file was created. (bigInt or number)
  • aTime: The Date object representing the file’s last access time. (Date)
  • mTime: The Date object representing the file’s last modified time. (Date)
  • cTime: The Date object representing the file’s last changed time. (Date)
  • birthtime: The Date object representing when the file was created. (Date)

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.


Last Updated : 26 Oct, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads