copy a file in javascript

To copy a file in JavaScript using Node.js, you can use the built-in fs module. This module provides a method called copyFile(), which takes the source file path and the destination file path as arguments. Here's an example:

index.tsx
const fs = require('fs');

const source = 'path/to/source/file.txt';
const destination = 'path/to/destination/file.txt';

fs.copyFile(source, destination, (err) => {
  if (err) throw err;
  console.log(`${source} was copied to ${destination}`);
});
249 chars
10 lines

In the above example, the copyFile() method is called with the source and destination file paths. The method also takes a callback function that is called once the copy is complete. If there's an error, it will be passed to the callback function as the first argument.

related categories

gistlibby LogSnag