How to Get Information About a File using Node.js ?
Last Updated :
02 Jul, 2024
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:
Property | Description | Type |
---|
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 |
---|
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 StructureExample: 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:
OutputSo by using the file system of nodeJS you can get all the required information about any file in the local file system.
Similar Reads
How to Add Data in JSON File using Node.js ? JSON stands for Javascript Object Notation. It is one of the easiest ways to exchange information between applications and is generally used by websites/APIs to communicate. For getting started with Node.js, refer this article.Prerequisites:NPM NodeApproachTo add data in JSON file using the node js
4 min read
How to Download a File Using Node.js? Downloading files from the internet is a common task in many Node.js applications, whether it's fetching images, videos, documents, or any other type of file. In this article, we'll explore various methods for downloading files using Node.js, ranging from built-in modules to external libraries.Using
3 min read
How to Copy a File in Node.js? Node.js, with its robust file system (fs) module, offers several methods to copy files. Whether you're building a command-line tool, a web server, or a desktop application, understanding how to copy files is essential. This article will explore various ways to copy a file in Node.js, catering to bot
2 min read
How To Read a File Line By Line Using Node.js? To read a file line by line in Node.js, there are several approaches that efficiently handle large files and minimize memory usage. In this article, we'll explore two popular approaches: using the Readline module (which is built into Node.js) and using the Line-reader module, a third-party package.
3 min read
How to Get File Character Encoding in Node.js ? Character encoding is essential when working with text files, as it determines how characters are represented in bytes. Different files may use different encodings, such as UTF-8, ASCII, or ISO-8859-1. Determining the character encoding of a file in Node.js can help ensure proper reading and process
2 min read