ItSolutionStuff.com

How to Move File in Folder using Node JS?

By Hardik Savani • September 23, 2021
Node JS

Hi,

Here, i will show you how to works how to move file in node js. Here you will learn node js move file if exists. you'll learn node js move files in folder. i would like to share with you node js move file sync. Follow bellow tutorial step of node js fs move file example.

We will use fs npm package for move file in folder using node.js. fs package provide rename() for move file. 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/laravel-doesnothave-users.png" path.

server.js

const fs = require('fs');

const filePath = './uploads/my-image.png';

const filePathMove = './images/my-image.png';

fs.rename(filePath, filePathMove, function(err) {

if ( err ) console.log('ERROR: ' + err);

console.log('File Move Successfully.');

});

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

How to Get File Size in Node JS?

Read Now →

How to Copy File to Another Directory in Node JS?

Read Now →

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 →