Agar Franny Agar Franny - 5 months ago 86
Javascript Question

How can I move files to a directory using Node.JS?

When I try

fs.move()
to move files to a directory, I get the following error:

/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


This is my code in file
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

Answer
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);
});