javascriptfileserverdeno

Reading file size and last modified date for file using Deno


How can I use Deno to read the size of a file and its last modified date/time?

In the browser I can use instanceOfFile.size and instanceOfFile.lastModified but these don't work if I provide the path to the file on the server.

const file = '/home/test/data.json'
const isFile = await fileExists(file)
if (isFile) {
  console.log(file.size)          // returns `undefined`
  console.log(file.lastModified). // returns `undefined`
}

Solution

  • You can use Deno.stat for that.

    const file = await Deno.stat("/home/test/data.json");
    if (file.isFile) {
      console.log("Last modified:", file.mtime?.toLocaleString());
      console.log("File size in bytes:", file.size);
    }