243

How can I move files (like mv command shell) on node.js? Is there any method for that or should I read a file, write to a new file and remove older file?

1
  • Depending on what you need you can use cpSync from fs module Commented Aug 15, 2023 at 17:38

18 Answers 18

236

According to seppo0010 comment, I used the rename function to do that.

http://nodejs.org/docs/latest/api/fs.html#fs_fs_rename_oldpath_newpath_callback

fs.rename(oldPath, newPath, callback)

Added in: v0.0.2

oldPath <String> | <Buffer> newPath <String> | <Buffer> callback <Function> 

Asynchronous rename(2). No arguments other than a possible exception are given to the completion callback.

Sign up to request clarification or add additional context in comments.

2 Comments

For those wondering where @seppo0010's comment went: it was on my answer, which I deleted and posted as a comment on the OP.
This will not work if you are crossing partitions or using a virtual filesystem not supporting moving files. You better use this solution with a copy fallback
117

Using nodejs natively

var fs = require('fs') var oldPath = 'old/path/file.txt' var newPath = 'new/path/file.txt' fs.rename(oldPath, newPath, function (err) { if (err) throw err console.log('Successfully renamed - AKA moved!') }) 

(NOTE: "This will not work if you are crossing partitions or using a virtual filesystem not supporting moving files. [...]" – Flavien Volken Sep 2 '15 at 12:50")

4 Comments

crossing partitions? what? you mean moving between filesystems?
It's an advanced way of connecting multiple servers, or using virtualization platforms like Docker, and what not, if you don't know it then you shouldn't be worry about it, the other user added that note to my answer, although I don't see the need for it in this context.
I think he rather meant moving between files systems, not just docker or network. It's important to add that. Just saying "partitions" is a bit confisuing, though.
doeso this work for files also
62

This example taken from: Node.js in Action

A move() function that renames, if possible, or falls back to copying

var fs = require('fs'); module.exports = function move(oldPath, newPath, callback) { fs.rename(oldPath, newPath, function (err) { if (err) { if (err.code === 'EXDEV') { copy(); } else { callback(err); } return; } callback(); }); function copy() { var readStream = fs.createReadStream(oldPath); var writeStream = fs.createWriteStream(newPath); readStream.on('error', callback); writeStream.on('error', callback); readStream.on('close', function () { fs.unlink(oldPath, callback); }); readStream.pipe(writeStream); } } 

6 Comments

Worked like a charm. Thanks! If I may add a little bit: 'move' might be a better name when it unlinks oldPath.
The copy() function is OK in this case, but if someone means to wrap it inside a Promise object, please either see my "answer" below or keep in mind to resolve the promise upon the "close" event on the write stream, not on the read stream.
This looks like something that'll work for my needs, however I don't know how to use the module.exports = function { } style. do I copy this code into my app itself where I already have var fs = require('fs'); and then call fs.move(oldFile, newFile, function(err){ .... }) instead of fs.rename ?
@Curious101 You can put this in a file like filemove.js and import it like var filemove = require('filemove'); then use it like filemove(...);
Thanks @Teomanshipahi. In that case I can add to mylibrary.js and use it from there. I thought this was some well known method of adding prototype methods so it becomes available in the object itself.
|
32

Use the mv node module which will first try to do an fs.rename and then fallback to copying and then unlinking.

3 Comments

Worked well for the simple requirements to move a file.
andrewrk appears to be the author of this mv node module. I like using npm to install ; npm install mv --save-dev; here's the npm link
How's this a dev dependency? Doesn't the app require mv in order to function?
22

The fs-extra module allows you to do this with its move() method. I already implemented it and it works well if you want to completely move a file from one directory to another - ie. removing the file from the source directory. Should work for most basic cases.

var fs = require('fs-extra') fs.move('/tmp/somefile', '/tmp/does/not/exist/yet/somefile', function (err) { if (err) return console.error(err) console.log("success!") }) 

2 Comments

I believe fs-extra returns promises as well so you can use async/await
fs-extra is a good choice because it correctly handles the case where it needs to move the file across devices (cases where rename fails, as cautioned in may other answers here, such as Docker volumes.) Code evidence here
20

util.pump is deprecated in node 0.10 and generates warning message

 util.pump() is deprecated. Use readableStream.pipe() instead 

So the solution for copying files using streams is:

var source = fs.createReadStream('/path/to/source'); var dest = fs.createWriteStream('/path/to/dest'); source.pipe(dest); source.on('end', function() { /* copied */ }); source.on('error', function(err) { /* error */ }); 

1 Comment

This is the proper way to copy/move a file that is on two different partitions. Thank you!
17

Using promises for Node versions greater than 8.0.0:

const {promisify} = require('util'); const fs = require('fs'); const {join} = require('path'); const mv = promisify(fs.rename); const moveThem = async () => { // Move file ./bar/foo.js to ./baz/qux.js const original = join(__dirname, 'bar/foo.js'); const target = join(__dirname, 'baz/qux.js'); await mv(original, target); } moveThem(); 

4 Comments

Just a word of caution fs.rename does not work if you are in a Docker environment with volumes.
Add an async declaration to the moveThem function.
You could also just do const { promises } = require("fs") and then use promises.rename (no need for util then)
this doesn't work if the original and target aren't on the same filesystem!
9

Using the rename function:

fs.rename(getFileName, __dirname + '/new_folder/' + getFileName); 

where

getFilename = file.extension (old path) __dirname + '/new_folder/' + getFileName 

assumming that you want to keep the file name unchanged.

1 Comment

Be careful this will not work if you try to rename the file between different partitions, neither on some virtual file systems (such as docker for instance)
7

fs.rename is also available in the sync version:

fs.renameSync(oldPath, newPath) 

Comments

5

Here's an example using util.pump, from >> How do I move file a to a different partition or device in Node.js?

var fs = require('fs'), util = require('util'); var is = fs.createReadStream('source_file') var os = fs.createWriteStream('destination_file'); util.pump(is, os, function() { fs.unlinkSync('source_file'); }); 

4 Comments

It's worth noting that you only have to do this when moving files across volumes. Otherwise, you can just use fs.rename() (within a volume renaming a file and moving it are the same thing).
Is possible to move file from local machine to server?
Nope, you need to use something else for that (like using FTP, HTTP or another protocol).
4

Just my 2 cents as stated in the answer above : The copy() method shouldn't be used as-is for copying files without a slight adjustment:

function copy(callback) { var readStream = fs.createReadStream(oldPath); var writeStream = fs.createWriteStream(newPath); readStream.on('error', callback); writeStream.on('error', callback); // Do not callback() upon "close" event on the readStream // readStream.on('close', function () { // Do instead upon "close" on the writeStream writeStream.on('close', function () { callback(); }); readStream.pipe(writeStream); } 

The copy function wrapped in a Promise:

function copy(oldPath, newPath) { return new Promise((resolve, reject) => { const readStream = fs.createReadStream(oldPath); const writeStream = fs.createWriteStream(newPath); readStream.on('error', err => reject(err)); writeStream.on('error', err => reject(err)); writeStream.on('close', function() { resolve(); }); readStream.pipe(writeStream); }) 

However, keep in mind that the filesystem might crash if the target folder doesn't exist.

Comments

3

I would separate all involved functions (i.e. rename, copy, unlink) from each other to gain flexibility and promisify everything, of course:

const renameFile = (path, newPath) => new Promise((res, rej) => { fs.rename(path, newPath, (err, data) => err ? rej(err) : res(data)); }); const copyFile = (path, newPath, flags) => new Promise((res, rej) => { const readStream = fs.createReadStream(path), writeStream = fs.createWriteStream(newPath, {flags}); readStream.on("error", rej); writeStream.on("error", rej); writeStream.on("finish", res); readStream.pipe(writeStream); }); const unlinkFile = path => new Promise((res, rej) => { fs.unlink(path, (err, data) => err ? rej(err) : res(data)); }); const moveFile = (path, newPath, flags) => renameFile(path, newPath) .catch(e => { if (e.code !== "EXDEV") throw new e; else return copyFile(path, newPath, flags) .then(() => unlinkFile(path)); }); 

moveFile is just a convenience function and we can apply the functions separately, when, for example, we need finer grained exception handling.

Comments

3

If you are fine with using an external library, Shelljs is a very handy solution.

command: mv([options ,] source, destination)

Available options:

-f: force (default behaviour)

-n: to prevent overwriting

const shell = require('shelljs'); const status = shell.mv('README.md', '/home/my-dir'); if(status.stderr) console.log(status.stderr); else console.log('File moved!'); 

1 Comment

Can you explain why this dependency is better than using the native Node.js APIs?
2

this is a rehash of teoman shipahi's answer with a slightly less ambiguous name, and following the design priciple of defining code before you attempt to call it. (Whilst node allows you to do otherwise, it's not good a practice to put the cart before the horse.)

function rename_or_copy_and_delete (oldPath, newPath, callback) { function copy_and_delete () { var readStream = fs.createReadStream(oldPath); var writeStream = fs.createWriteStream(newPath); readStream.on('error', callback); writeStream.on('error', callback); readStream.on('close', function () { fs.unlink(oldPath, callback); } ); readStream.pipe(writeStream); } fs.rename(oldPath, newPath, function (err) { if (err) { if (err.code === 'EXDEV') { copy_and_delete(); } else { callback(err); } return;// << both cases (err/copy_and_delete) } callback(); } ); } 

Comments

1

There's a native async API available since NodeJS v14:

import fs from 'fs/promises'; await fs.rename(oldPath, newPath); 

Comments

-1

If you are trying to move or rename a node.js source file, try this https://github.com/viruschidai/node-mv. It will update the references to that file in all other files.

Comments

-1

With the help of below URL, you can either copy or move your file CURRENT Source to Destination Source

https://coursesweb.net/nodejs/move-copy-file

/*********Moves the $file to $dir2 Start *********/ var moveFile = (file, dir2)=>{ //include the fs, path modules var fs = require('fs'); var path = require('path'); //gets file name and adds it to dir2 var f = path.basename(file); var dest = path.resolve(dir2, f); fs.rename(file, dest, (err)=>{ if(err) throw err; else console.log('Successfully moved'); }); }; //move file1.htm from 'test/' to 'test/dir_1/' moveFile('./test/file1.htm', './test/dir_1/'); /*********Moves the $file to $dir2 END *********/ /*********copy the $file to $dir2 Start *********/ var copyFile = (file, dir2)=>{ //include the fs, path modules var fs = require('fs'); var path = require('path'); //gets file name and adds it to dir2 var f = path.basename(file); var source = fs.createReadStream(file); var dest = fs.createWriteStream(path.resolve(dir2, f)); source.pipe(dest); source.on('end', function() { console.log('Succesfully copied'); }); source.on('error', function(err) { console.log(err); }); }; //example, copy file1.htm from 'test/dir_1/' to 'test/' copyFile('./test/dir_1/file1.htm', './test/'); /*********copy the $file to $dir2 END *********/

Comments

-6

Node.js v10.0.0+

const fs = require('fs') const { promisify } = require('util') const pipeline = promisify(require('stream').pipeline) await pipeline( fs.createReadStream('source/file/path'), fs.createWriteStream('destination/file/path') ).catch(err => { // error handling }) fs.unlink('source/file/path') 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.