Categories
JavaScript Answers

How to rename files using Node.js?

Spread the love

Sometimes, we want to rename files using Node.js.

In this article, we’ll look at how to rename files using Node.js.

How to rename files using Node.js?

To rename files using Node.js, we can use the fs.rename method.

For instance, we write

const fs = require("fs");

fs.rename("/path/to/Afghanistan.png", "/path/to/AF.png", (err) => {
  if (err) {
    console.log(err);
  }
});

to call fs.rename with the source and destination paths and a callback that runs after rename is done running.

If there’re errors with renaming files, then err is set.

Conclusion

To rename files using Node.js, we can use the fs.rename method.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *