How to get the last updated date of a file using Node.js

By

Learn how to get the last modified date of a file in Node.js by calling fs.statSync on the path and reading the mtime Date property it returns.

~~~

To get the last updated date of a file in Node.js, call fs.statSync() on the file path and read the mtime property of the object it returns.

All the file functions in Node.js are provided by the fs module. This module exposes a method called statSync(), which gets the file details synchronously.

By calling it passing a file path (relative to the file location, or absolute), it will return an object that contains the mtime property.

That is a Date object instance that contains the file last modified date.

const fs = require('fs')

const getFileUpdatedDate = (path) => {
  const stats = fs.statSync(path)
  return stats.mtime
}

getFileUpdatedDate('notes.txt')
// 2026-08-07T20:35:26.249Z

Since it’s a Date, you can format it however you like, or compare it with another date. Check out the JavaScript Date guide to find out more how to handle the Date object, if you need.

The other dates on the stats object

mtime is the modification time: it changes every time the file content is written.

The stats object carries a few more dates:

For a “last updated” label on a page, mtime is the one you want.

If you’d rather work with a number than a Date, there’s also mtimeMs, the same instant expressed in milliseconds since the Unix epoch. Handy for sorting a list of files by their last change.

The async version

statSync() blocks until the disk answers. That’s fine in a script or at build time.

Inside a server handling requests, use the promise-based version instead:

const fs = require('fs/promises')

const getFileUpdatedDate = async (path) => {
  const stats = await fs.stat(path)
  return stats.mtime
}

Same result, but the event loop stays free while Node waits for the filesystem.

Watch out for missing files

If the file does not exist, statSync() throws an error with the code ENOENT, and that will crash your program if nothing catches it.

Wrap the call in a try/catch when the file might not be there:

try {
  const stats = fs.statSync('notes.txt')
  console.log(stats.mtime)
} catch (err) {
  if (err.code === 'ENOENT') {
    console.log('file not found')
  }
}

This is better than checking with fs.existsSync() first, because the file could disappear between the check and the statSync() call.

Tagged: Node.js · All topics
~~~

Related posts about node: