夏期劇場
夏期劇場

Reputation: 18325

NodeJS File Downloader with WGET?

I'm trying to make File Downloads with WGET method of nodejs. I found this:

var exec = require('exec');

// Function to download file using wget
var download_file_wget = function(file_url) {

    // extract the file name
    var file_name = url.parse(file_url).pathname.split('/').pop();
    // compose the wget command
    var wget = 'wget -P ' + DOWNLOAD_DIR + ' ' + file_url;
    // excute wget using child_process' exec function

    var child = exec(wget, function(err, stdout, stderr) {
        if (err) throw err;
        else console.log(file_name + ' downloaded to ' + DOWNLOAD_DIR);
    });
};

But it says:

Error: Cannot find module 'exec'

Is exec an another module to be installed and imported .. or how can i make it work?

Upvotes: 1

Views: 2601

Answers (1)

jimr
jimr

Reputation: 11230

Yes, url is one of the built-in node modules

Just do

var url = require('url');

somewhere in your file.

exec is part of child_process so to get it do

var exec = require('child_process').exec;

Upvotes: 3

Related Questions