How to Move File in Folder using Node JS?

About Us:

We are from free coder. We are a group of web developers who are passionate about web development. You can learn from us php, laravel, node js, vue js, react js and many other topic on web developement.
we are very happy to help you.
free coder

Today what you are going to learn:

Hello Brother,

Hello Brother, 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 lesson.

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 lesson

Lesson 1: Create Node App

run bellow command and create node app.

mkdir my-app

cd my-app

npm init

Lesson 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

Result:-

thank you very much for following up with me.

Tags

Recent Posts

Related Posts