如何使用Node.JS将文件移动到目录?

时间:2016-07-09 19:26:34

标签: javascript node.js

当我尝试fs.move()将文件移动到目录时,我收到以下错误:

/home/decuge31/node_modules/fs-extra/lib/move/index.js:19                                                                                                                    |
  var shouldMkdirp = ('mkdirp' in options) ? options.mkdirp : true                                                                                                           |
                                  ^                                                                                                                                          |
TypeError: Cannot use 'in' operator to search for 'mkdirp' in undefined                                                                                                      |
    at Object.mv [as move] (/home/decuge31/node_modules/fs-extra/lib/move/index.js:19:35)                                                                                    |
    at Object.<anonymous> (/home/decuge31/bscBot.js:8:4)                                                                                                                     |
    at Module._compile (module.js:456:26)                                                                                                                                    |
    at Object.Module._extensions..js (module.js:474:10)                                                                                                                      |
    at Module.load (module.js:356:32)                                                                                                                                        |
    at Function.Module._load (module.js:312:12)                                                                                                                              |
    at Function.Module.runMain (module.js:497:10)                                                                                                                            |
    at startup (node.js:119:16)                                                                                                                                              |
    at node.js:902:3 

这是我在bscBot.js文件中的代码:

var fs = require('fs-extra'); // This imports the fs-extra dependency
var request = require('request'); // This imports the request dependency

fs.ensureDir('css'); // This creates a folder called css

request('http://agar.io/css/master.css').pipe(fs.createWriteStream('master.css')); // This downloads the master.css file

fs.move('master.css', 'css'); // This moves master.css to the specified directory, in this case it is called css

3 个答案:

答案 0 :(得分:1)

var fs = require('fs-extra');
var request = require('request');
fs.ensureDir('css');
request('http://agar.io/css/master.css').pipe(fs.createWriteStream('master.css'));
fs.move('master.css', 'css/master.css', function (err) {
  if (err)
      console.error(err);
});

答案 1 :(得分:1)

您需要等到管道结束才能在移动文件之前写入流中。

你可以试试这个:

var fs = require('fs-extra') // This imports the fs-extra dependency
var request = require('request') // This imports the request dependency

fs.ensureDirSync('css') // This creates a folder called css

var stream = request('http://agar.io/css/master.css').pipe(fs.createWriteStream('master.css')) // This downloads the master.css file

stream.on('end', function() {
        fs.move('master.css', 'css')
});

答案 2 :(得分:0)

尝试fs.move('master.css', 'css/master.css')