ItSolutionStuff.com

How to Get File Size in Node JS?

By Hardik Savani • September 21, 2021
Node JS

Hi,

This article will give you example of how to get file size in node js. you will learn node js get file size. We will look at example of node js get file size in mb. i would like to share with you how to check file size in node js.

We will use fs npm package for find file size using node.js. fs package provide statSync() for getting file size in mb. let's see simple example

Step 1: Create Node App

run bellow command and create node app.

mkdir my-app

cd my-app

npm init

Step 2: Create server.js file

Make sure, you have add file on "uploads/best-open-source.jpg" path.

server.js

const fs = require('fs');

const filePath = './uploads/best-open-source.jpg';

var fileInfo = fs.statSync(filePath);

var fileSize = fileInfo.size;

var fileSizeMB = fileInfo.size / (1024*1024);

console.log('File Size:' + fileSizeMB);

console.log('File Size in MB:' + fileSizeMB);

now you can simply run by following command:

node server.js

Output:

i hope it can help you...

Tags: Node JS
Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

Node JS Rename All Files in Folder Example

Read Now →

How to Rename File in Folder using Node JS?

Read Now →

How to Delete Directory in Node.js?

Read Now →

Node JS - How to Delete All Files in Directory?

Read Now →

How to Check File is Exist or Not in Node JS?

Read Now →

How to Create Directory if does not Exists in Node JS?

Read Now →

How to Delete File If Exists in Node JS?

Read Now →

How to Get All Files from Folder in Node JS?

Read Now →

Node JS Sort Array of Objects by Value Example

Read Now →

Node JS Http Request with Headers Example

Read Now →