ItSolutionStuff.com

How to Copy File to Another Directory in Node JS?

By Hardik Savani • September 20, 2021
Node JS

Hi,

Now, let's see example of node js copy file to another directory. this example will help you how to copy file in node js. i would like to show you node js copy files in directory. We will use nodejs copy file overwrite.

We will use fs npm package for copy file in folder using node.js. fs package provide copyFile() for coping 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/my-image.png" path.

server.js

const fs = require('fs');

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

const filePathCopy = './images/my-image-copy.png';

fs.copyFile(filePath, filePathCopy, (err) => {

if (err) throw err;

console.log('File Copy 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 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 →

How to Create and Use .env File in Node JS?

Read Now →

Axios HTTP requests in Node JS Example

Read Now →

Node JS Make HTTP Put Request Example

Read Now →