What is move-file?
The move-file npm package is a utility for moving files and directories with ease. It supports various features such as overwriting existing files, renaming files, and ensuring the destination directory exists.
What are move-file's main functionalities?
Basic File Move
This feature allows you to move a file from one location to another. The code sample demonstrates moving 'source.txt' to 'destination.txt'.
const moveFile = require('move-file');
(async () => {
await moveFile('source.txt', 'destination.txt');
console.log('The file has been moved');
})();
Overwrite Existing File
This feature allows you to move a file and overwrite the destination file if it already exists. The code sample demonstrates moving 'source.txt' to 'destination.txt' with the overwrite option enabled.
const moveFile = require('move-file');
(async () => {
await moveFile('source.txt', 'destination.txt', { overwrite: true });
console.log('The file has been moved and overwritten');
})();
Ensure Destination Directory Exists
This feature ensures that the destination directory exists before moving the file. If the directory does not exist, it will be created. The code sample demonstrates moving 'source.txt' to a nested directory structure.
const moveFile = require('move-file');
(async () => {
await moveFile('source.txt', 'non/existent/directory/destination.txt');
console.log('The file has been moved and the destination directory was created');
})();
Other packages similar to move-file
fs-extra
fs-extra is a package that extends the native Node.js fs module with additional methods, including file moving capabilities. It provides a more comprehensive set of file system operations compared to move-file, such as copying, removing, and ensuring directories exist.
mv
mv is a simple package for moving files and directories. It offers similar functionality to move-file but with a simpler API. It also supports overwriting existing files and ensuring the destination directory exists.
rename
rename is a package focused on renaming files and directories. While it can be used to move files by renaming them to a new path, it lacks some of the additional features provided by move-file, such as ensuring the destination directory exists.
move-file
Move a file
The built-in fs.rename()
is just a JavaScript wrapper for the C rename(2)
function, which doesn't support moving files across partitions or devices. This module is what you would have expected fs.rename()
to be.
Highlights
- Promise API.
- Supports moving a file across partitions and devices.
- Optionally prevent overwriting an existing file.
- Creates non-existent destination directories for you.
Install
$ npm install move-file
Usage
const moveFile = require('move-file');
(async () => {
await moveFile('source/unicorn.png', 'destination/unicorn.png');
console.log('The file has been moved');
})();
API
moveFile(source, destination, [options])
Returns a Promise
that resolves when the file has been moved.
moveFile.sync(source, destination, [options])
source
Type: string
File you want to move.
destination
Type: string
Where you want the file moved.
options
Type: Object
overwrite
Type: boolean
Default: true
Overwrite existing destination file.
Related
- cp-file - Copy a file
- cpy - Copy files
- make-dir - Make a directory and its parents if needed
License
MIT © Sindre Sorhus