← coderrocketfuel.com

Move a System File To a New Directory with Node.js

Are you working with system files in Node.js and need an easy way to move files programmatically?

Luckily, Node.js has a built-in way to do this with their File System (Fs) core module, which has both a fs.rename() and fs.renameSync() method to give a file a new name.

This rename method will allow us to change the path of our file to something new, thus moving it to the new location.

Both of the move functions will give you the same result, but go about doing it in a little different way. The synchronous fs.renameSync() version will stop your code and wait until the file has been successfully renamed or an error has occurred to continue running. And the asynchronous version fs.rename() will not block your code and return a callback function when the file is removed instead.

We'll show you how to use both of these methods.

For the code below to work, make sure you have Node.js installed and a file you want to rename placed in the root of your project directory. In this example, our file is a PNG image file named "your-file.png".

fs.rename()

First, let's cover the fs.rename() version. We'll give you the full code and then explain all the different parts afterward:

const fs = require("fs")
const path = require("path")

const currentPath = path.join(__dirname, "your-file.png")
const newPath = path.join(__dirname, "your-directory", "new-filename.png")

fs.rename(currentPath, newPath, function(err) {
  if (err) {
    throw err
  } else {
    console.log("Successfully moved the file!")
  }
})

Let's break down each part of the code:

  1. First, we import the fs module and path core modules.
  2. Next, we create the currentPath and newPath variables. We use the path module to get the current path of our file we want to move. And we also create a new path to move the file to.
  3. Then, we use the fs.rename() function. We pass both file paths to the function and it returns a callback.
  4. 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 moved the file!

You should see the file moved to its new location.

Now let's cover the synchronous version!

fs.renameSync()

Here's the full code for this method:

const fs = require("fs")
const path = require("path")

const currentPath = path.join(__dirname, "your-file.png")
const newPath = path.join(__dirname, "your-directory", "your-file.png")

try {
  fs.renameSync(currentPath, newPath)
  console.log("Successfully moved the file!")
} catch(err) {
  throw err
}

Similar to the previous example, we require both the fs and path core modules. Then, we get the path to the current file and create a new path to where we want to move the file.

But then we use a try...catch statement. In the try section, we pass both the currentPath and newPath variables to the fs.renameSync() function and log a success message when the file has successfully been 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 moved the file!

You should see the file moved to its new location.

Hopefully this was helpful in your work with system files and Node.js.