How to get file size using nodejs

By Yashwant Chavan, Views 14436, Date 20-Feb-2017

In this tutorial, You will learn how to get file size using nodejs.

tags nodejs

Node.js file size example

require('fs') - Load the File System module

fs.statSync(path) method returns the instance of fs.Stats

fs.Stats contains below properties, out of that, you can use size property to get the file size.

{
  dev: 2114,
  ino: 48064969,
  mode: 33188,
  nlink: 1,
  uid: 85,
  gid: 100,
  rdev: 0,
  size: 527,
  blksize: 4096,
  blocks: 8,
  atime: Mon, 10 Oct 2011 23:24:11 GMT,
  mtime: Mon, 10 Oct 2011 23:24:11 GMT,
  ctime: Mon, 10 Oct 2011 23:24:11 GMT,
  birthtime: Mon, 10 Oct 2011 23:24:11 GMT
}
var fs = require('fs');
var stats = fs.statSync("c:\\eclipse-jee-mars-1-win32-x86_64.zip");
console.log('File Size in Bytes:- ' + stats.size);

output

File Size in Bytes:- 288024166
Yashwant Chavan

Yashwant Chavan

Hi there! I am founder of technicalkeeda.com and programming enthusiast. My skills includes Java,J2EE, Spring Framework, Nodejs, PHP and lot more. If you have any idea that you would want me to develop? Lets connect: yashwantchavan@gmail.com