How to Get Image File Size in Node Js using FS Module

Last updated on: by Digamber

If you are fond of programming, we are damn sure you always look for the process that makes web development facile, as well as efficient with reusable coding paradigm kept in mind.

Node is the framework that makes all of your wishes come true through its flexible and robust environment.

Today in this short guide, we will demystify the process that helps you work with files in Node js. We will elaborate on how to eloquently get the image file size in the Node js application from scratch.

We will show you how to determine the image size in kilobytes (KB) and Megabytes (MB) in node js using the FS module.

In node FS module stands for the file system; it truly gives you the power to work with files deeply.

The file system module is more powerful than you thought; it allows interacting with the files in node super easily. The file system makes your development less cumbersome by providing support for Promise, Async, and Callback APIs.

Node JS Obtain Image File Size using File System Module Example

  • Step 1: Make App Folder
  • Step 2: Generate Package File
  • Step 3: Create Node Script
  • Step 4: Register Script in Node
  • Step 5: Find File Size

Make App Folder

Here is the first instruction, which tells you to create a blank folder for keeping the code for this simple feature.

Make sure to open the code editor, then open the terminal type the command and press enter.

Likewise, you can choose to create the directory manually.

mkdir node-galaxy

Do not forget to get inside the folder.

cd node-galaxy

Generate Package File

Now, you are into the blank directory now you need a file that keeps the node project information.

For this task, we can use a single command that generates package.json file. Make sure to provide the information asked by cli tool.

npm init

You can now see the file manifested in your node project.

Create Node Script

Its the time when you have to create the script that handles the process of knowing the file size.

Therefore, in your project directory, create the app.js file, insert the following code within the file.

You need to keep the demo image in the node project’s root.

const fs = require('fs')
const imgPath = './samurai.jpg'
let imgInfo = fs.statSync(imgPath)
let fileSize = imgInfo.size
let fileSizeMB = imgInfo.size / (1024 * 1024)
console.log('File size in kb:' + fileSize)
console.log('File size in mb:' + fileSizeMB)

Register Script in Node

We have to register the node script in scripts array in the package.json file. This process allows you to execute the script from command line interface.

Inside the script section in the package.json file make sure to add the script name as suggested below.

  "scripts": {
    "start": "node app.js"

Find File Size

Finally we are ready to see the output that extracts the file size in node.

From the console execute the given command.

node app.js

Here is the file size that you are currently seeing.

File size in kb: 135382
File size in mb: 0.12911033630371094


How to Get Image File Size in Node Js using FS Module

In this post, we have learned how to find out the image file size in Node using the local path or URL.

To find the image size from URL from local URL, we took the help of file system module moreover we saw how to use the easy method of fs module diligently.

We accessed the statSync() method through the fs module, as per the standard Node documentation. The statSync method helps to gather details about the given file path synchronously.

We learned to use the Node’s fs module and successfully extracted the image/file size in kb and MB in Node.


I am Digamber, a full-stack developer and fitness aficionado. I created this site to bestow my coding experience with newbie programmers. I love to write on JavaScript, ECMAScript, React, Angular, Vue, Laravel.