Node Js Asynchronously Copy File to New Folder Tutorial

How do you copy the system file and move it to another directory in Node?

Don’t know? Fret not. This quick yet comprehensive tutorial is here to help you.

In this tutorial, we will teach you how to simply and swiftly copy files and move files to another directory in the Node js application.

The popularity of Node.js is on top; Node is a powerful JavaScript runtime environment. It is an open-source, cross-platform, back-end JavaScript runtime environment that works on the V8 engine.

It is primarily used to build cool apps, invokes JavaScript code outside a web browser, and offers tons of APIs that make your back-end development super easy.

In this guide, we will look at a simple problem referred to as copy file overwrite using a built-in node fs.copyFile() method.

If you want to copy a system file from the source path to another directory asynchronously. The fs.copyFile() method helps in such type of requirement.

If you use the fs.copyFile() function, it will overwrite the file if it finds it already exists at the given path.

Here is the method with default parameters that provides the fastest way to copy a file in node.js application.

fs.copyFile(source, destination, mode, callback)

Let us dive into the node file system tutorial and find out how to copy files in Node programmatically.

Node JS Copy File and Move into Different Directory Example

  • Step 1: Create Node Folder
  • Step 2: Set Up Node Project
  • Step 3: Set Up Server.js File
  • Step 4: Start Node Server

Create Node Folder

We would like to urge you to open the terminal or command-line app.

You can take the help of a terminal app, or you may use your code editor’s default terminal app.

After you open the terminal, use the following command.

mkdir node-backend

If you want to name something else to your node app. You are more than welcome.

Now, look for the node folder on your computer’s screen, after that jump into the node app folder.

Set Up Node Project

A node project is instantiated using the default npm command. So, one more time, write the given command on your terminal.

There after, hit enter to begin the node app forming process.

npm init

After you evoke the npm initializer command, you will see a couple of options on your screen.

It primarily asks for the name, version, description, author, and license; make sure to add the information.

Now, you have the new package.json file generated on your node project, and you are fully ready to set up a new or existing npm packages in new app.

If you want to know more about npm, visit here.

Set Up Server.js File

In your node project folder, create `uploads` and `img` folders. In the uploads folder, keep an image file. We are using the static image; later, you can set the dynamic image.

Define a variable with a new destination path. Pass current image path and new folder path variables in the fs.copyFile() method.

const fs = require('fs');
  
const PathToFile = './uploads/star-trek.jpeg';
const copyFilePath = './img/star-trek-copied.jpeg';
    
fs.copyFile(PathToFile, copyFilePath, (err) => {
  if (err) {
    throw err
  } else {
    console.log('File successfully copied and moved to new directory.');    
  }
});

Start Node Server

Let us check out how our little node app is working; before we begin, we must go to the command prompt.

On the console, screen type the node command followed by the primary file name.

Press enter to copy the file to another directory.

node server.js

This is how you will see the response on your terminal screen.

File successfully copied and moved to new directory.

Node Js Asynchronously Copy File to New Folder Tutorial

Summary

In this Node system files tutorial, we talked about copying files to a new directory, a small yet useful feature. Software is always built by adding multiple small pieces; we assume this guide will help your development journey.