Extracting information about a file in Node.js can be useful in many situations, such as monitoring the state of a file, checking permissions, or simply analyzing its metadata. Node.js offers several built-in methods and libraries to retrieve detailed information about files, such as size, type, modification date, and much more. In this article, we’ll see how to use these tools to easily obtain file information.
The main module for handling files in Node.js is fs
(file system). This module is included by default in Node.js, so there is no need to install any additional packages. The fs
module offers various functions for managing files and directories.
The fs.stat()
function is one of the most useful for obtaining information about a file. fs.stat()
provides various file metadata, such as size, modification date, permissions, and much more.
const fs = require('fs');
fs.stat('path/to/file.txt', (err, stats) => {
if (err) {
console.error('Error:', err);
return;
}
console.log('File Information:');
console.log('Size:', stats.size); // File size in bytes
console.log('Is a file:', stats.isFile());
console.log('Is a directory:', stats.isDirectory());
console.log('Creation date:', stats.birthtime); // Creation date
console.log('Last modification date:', stats.mtime); // Last modification date
});
Key properties of stats
:
- size: the file size in bytes.
- isFile(): returns
true
if it’s a file. - isDirectory(): returns
true
if it’s a directory. - birthtime: file creation date.
- mtime: last modification date.
If you prefer to work with Promises
or use async/await
, you can use fs.promises.stat
, which returns a Promise
. This syntax is often more readable, especially for asynchronous code.
const fs = require('fs').promises;
async function getFileStats(filePath) {
try {
const stats = await fs.stat(filePath);
console.log('File Information:');
console.log('Size:', stats.size);
console.log('Is a file:', stats.isFile());
console.log('Is a directory:', stats.isDirectory());
console.log('Creation date:', stats.birthtime);
console.log('Last modification date:', stats.mtime);
} catch (error) {
console.error('Error:', error);
}
}
getFileStats('path/to/file.txt');
The fs
module also allows you to check a file’s permissions through the mode
property of fs.stat()
. This property contains a numeric value representing the file’s permissions.
fs.stat('path/to/file.txt', (err, stats) => {
if (err) {
console.error('Error:', err);
return;
}
const permissions = stats.mode & 0o777; // Extracts permissions in octal format
console.log('Permissions:', permissions.toString(8)); // Converts to octal string
});
Here, 0o777
is an octal mask that selects only the permissions. The result will be an octal string like 644
or 755
, representing read, write, and execute permissions for the user, group, and other users.
Conclusion
Node.js offers several powerful tools for working with files and retrieving the necessary information. The functionalities of the fs
module provide a solid foundation for extracting detailed file information.