
file contains content and type. Apart from this, you will have metadata of a file
It is very easy to get the file information in Operating System such as windows.
How do you get file metadata information in Nodejs?
Nodejs provides inbuilt module - fs
and it contains stat()
and statSync()
method to get metadata properties of an file or a directory.
It provides two methods
- fs.statSync method
- fs.start method
How to get file metadata such as size created date
There are two ways we can get it done.
nodejs fs stat method
stat() is an asynchronous operation for reading the file and printing the object of metadata.
fs.stat(path, options, callbackfunction)
path
is a file or directory absolute or relative path
options
are optional parameters passed to customize the output, bigint is false or true. true means numeric values returned by this option
callback
is a function which contains first parameter error in case of errors, second parameter an object return file metadata object.
In Nodejs asynchronous operations contains a callback
const fs = require("fs")
fs.stat("./package.json", (err, fileObject) => {
if (err) {
console.log(err)
} else {
console.log(fileObject)
}
})
Output:
Stats {
dev: 3330445139,
mode: 33206,
nlink: 1,
uid: 0,
gid: 0,
rdev: 0,
blksize: 4096,
ino: 562949955554406,
size: 204,
blocks: 0,
atimeMs: 1645280252542.7854,
mtimeMs: 1644120598975.729,
ctimeMs: 1644120598975.729,
birthtimeMs: 1642860250548.1833,
atime: 2022-02-19T14:17:32.543Z,
mtime: 2022-02-06T04:09:58.976Z,
ctime: 2022-02-06T04:09:58.976Z,
birthtime: 2022-01-22T14:04:10.548Z
}
Here are details of a returned object
dev
: device identifier in numeric,
mode
: file type in bits,
nlink
:hard links count,
uid
: file owner identifier,
gid
: group identifier of an owner for a file
rdev
: special file identifier
blksize
: file system block size,
ino
: inode number for a file,
size
: file size in bytes,
blocks
: blocks allocated for a file,
atimeMs
: last accessed timestamp in milliseconds
mtimeMs
: file modified timestamp in milliseconds
ctimeMs
: file status changed timestamp in milliseconds
birthtimeMs
: created timestamp in milliseconds
atime
: last accessed Date format with timezone information
mtime
: file modified Date format with timezone information
ctime
: file changed Date format with timezone information,
birthtime
: file created Date format with timezone information
It provides the following methods
const fs = require("fs")
fs.stat("./package.json", (err, fileObject) => {
if (err) {
console.log(err)
} else {
console.log(fileObject.isFile()) // return true for files
console.log(fileObject.isDirectory())// true for directory
console.log(fileObject.isBlockDevice())
console.log(fileObject.isSymbolicLink()) return true for symbolic link
console.log(fileObject.isCharacterDevice())
console.log(fileObject.isFIFO())
console.log(fileObject.isSocket())
}
})
Output:
true
false
false
false
false
false
false
Nodejs fs statSync function
statSync is a synchronization version of the start function. It executes sequentially and waits for execution to finish before going to the next line.
Here is an syntax fs.statSync(path, options)
path
is absoluate or relative path of an resource(file,directory)
options
contains javascript with bigint is key and value is true/false. if true, output returns numbers in biginteger format.
const fileInfo = fs.statSync('./package.json')
console.log(fileInfo)
It