How do I move file a to a different partition or device in Node.js?

后端 未结 5 1163
终归单人心
终归单人心 2020-11-30 04:36

I\'m trying to move a file from one partition to another in a Node.js script. When I used fs.renameSync I received Error: EXDEV, Cross-device link.

5条回答
  •  难免孤独
    2020-11-30 05:31

    I made a Node.js module that just handles it for you. You don't have to think about whether it's going to be moved within the same partition or not. It's the fastest solution available, as it uses the recent fs.copyFile() Node.js API to copy the file when moving to a different partition/disk.

    Just install move-file:

    $ npm install move-file
    

    Then use it like this:

    const moveFile = require('move-file');
    
    (async () => {
        await moveFile(fromPath, toPath);
        console.log('File moved');
    })();
    

提交回复
热议问题