← coderrocketfuel.com

How to Get the Stats for a File in Node.js

How do you get the stats for a file using Node.js?

Node.js has a built-in module called Fs that provides an fs.stat() method that lists all of the information for a given file, such as the file size, created date, and much more.

The fs.stat() method takes a path to a file and returns an object with all of the data for the given file.

Make sure you have Node.js installed on your local development machine. If you need one, we created a guide on installing Node.js on your machine.

Before we start working with the code, you'll also need a file to work with. So, add one to the root of your project. It can be any type of file you want, such as a JPEG, PNG, PDF, etc.

Once you have a file to work with, open a Node.js file and add this code to it:

const fs = require("fs")

fs.stat("./file.jpg", (err, fileStats) => {
  if (err) {
    console.log(err)
  } else {
    console.log(fileStats)
  }
})

Let's go over each part of that code.

The first thing we do is import the Fs core module. Since this is one of the Node.js core modules, we don't need to install anything and we can simple require() it and use it right away.

Then, we use the fs.stat() function. The first parameter to the function is a path to the file you want stats for and the second is a callback function that returns an object with all the stats contained in it.

We also do some error handling inside the callback function so an error is logged if one occurs.

The fileStats object returned by the function will look similar to this:

{
  dev: 2066,
  mode: 33204,
  nlink: 1,
  uid: 1000,
  gid: 1000,
  rdev: 0,
  blksize: 4096,
  ino: 12204484,
  size: 81151,
  blocks: 160,
  atimeMs: 1570093239638.8958,
  mtimeMs: 1566558056262.4106,
  ctimeMs: 1570093248922.558,
  birthtimeMs: 1570093248922.558,
  atime: 2019-10-03T09:00:39.639Z,
  mtime: 2019-08-23T11:00:56.262Z,
  ctime: 2019-10-03T09:00:48.923Z,
  birthtime: 2019-10-03T09:00:48.923Z
}

And here's a short description of what each item in the object means:

  • dev: numeric identifier for the device containing the file.
  • mode: bit-field describing the file type and mode.
  • nlink: number of hard links that exist for the file.
  • uid: numeric identifier for the user that owns the file.
  • gid: numberic identifer for the user group that owns the file.
  • rdev: numeric identifier for if the file is considered "special".
  • blksize: file system block size for i/o operations.
  • ino: file system specific "lnode" number for the file.
  • size: size of the file in bytes.
  • blocks: number of blocks allocated for the file.
  • atimeMs: timestamp for the last time the file was accessed.
  • mtimeMs: timestamp for the last time the file was modified (in milliseconds).
  • ctimeMs: timestamp for the last time the file status was changed (in milliseconds).
  • birthtimeMs: timestamp for when the file was created (in milliseconds).
  • atime: timestamp for the last time the file was accessed (Date object).
  • mtime: timestamp for the last time the file was modified (Date object).
  • ctime: timestamp for the last time the file status was changed (Date object).
  • birthtime: timestamp for when the file was created (Date object).

You can access each specific item the same way you would with any object. Below are a few examples:

fileStats.size // i.e. 81151
fileStats.atimeMs // i.e. 1570093239638.8958
fileStats.birthtime // i.e. 2019-10-03T09:00:48.923Z

Hopefully, this was helpful in your coding endeavors and helped in your dealings with files.

Thanks for reading and happy coding!