Copy a System File & Move it To a New Directory in Node.js
Do you need to make a copy of a system file using Node.js? And do you need to then move the file to a new directory? This article will show you how to do that.
Node.js has a built-in way to do this with their File System (Fs) core module, which has both a fs.copyFile()
and fs.copyFileSync()
method to copy a file.
The synchronous fs.copyFileSync()
version will stop your code and wait until the file has been copied or an error occurred to continue. And the asynchronous version fs.copyFile()
will not block your code and return a callback function when the file is done being copied.
We'll show you how to use both examples.
For the code below to work, make sure you have Node.js installed and a file you want to copy placed in the root of your project directory. For testing purposes, also make sure you have a directory created called backups
where the copied file will be moved to.
Table Of Contents
fs.copyFile()
First, let's go over the fs.copyFile()
approach.
Here's what the code will look like:
const fs = require("fs")
const path = require("path")
const pathToFile = path.join(__dirname, "your-file.png")
const pathToNewDestination = path.join(__dirname, "backups", "your-file-copy.png")
fs.copyFile(pathToFile, pathToNewDestination, function(err) {
if (err) {
throw err
} else {
console.log("Successfully copied and moved the file!")
}
})
Let's break down each part of the code:
- First, we import the
fs
andpath
core modules. - Next, we create the
pathToFile
andpathToNewDestination
variables. We use thePath
module to get the current path of our file we want to change. And we also create a path for where we want the copy of the file to be moved to. - Then, we use the
fs.copyFile()
function. We pass both file paths to the function and it returns a callback. - Inside the callback function, we do some error handling and then, if successful, we
console.log()
a success message.
When you run the code in your terminal, you should see this output:
Successfully copied and moved the file!
If you look in a backups
directory where the file is located, you should see that the copied file is in there.
fs.copyFileSync()
Now let's cover the synchronous version!
Below is what the code looks like:
const fs = require("fs")
const path = require("path")
const pathToFile = path.join(__dirname, "your-file.png")
const pathToNewDestination = path.join(__dirname, "backups", "your-file-copy.png")
try {
fs.copyFileSync(pathToFile, pathToNewDestination)
console.log("Successfully copied and moved the file!")
} catch(err) {
throw err
}
As with the previous example, we require both the fs
and path
core modules. Then, we get the path to the current file and create a path with what we want the new name to be.
But then we use a try...catch
statement. In the try
section, we pass both the pathToFile
and pathToNewDestination
variables to the fs.copyFileSync()
function and log a success message when the file has successfully been copied and moved. And we use the catch
section to throw any errors that occur along the way.
When you run the code in your terminal, you should see the same output as before:
Successfully copied and moved the file!
And your file should be copied and moved to a backups
directory.
Hopefully, this was helpful in your work with system files and Node.js.