Node.js - The “listener” argument must be of type

2019-08-19 05:57发布

问题:

I am trying to create a proxy update code with node.js, and im getting this error:

    events.js:180
    throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'listener', 'Function');
    ^

TypeError [ERR_INVALID_ARG_TYPE]: The "listener" argument must be of type Function
    at _addListener (events.js:180:11)
    at WriteStream.addListener (events.js:240:10)
    at WriteStream.close (fs.js:2298:10)
    at WriteStream.<anonymous> (/Users/camden/Desktop/proxyupdate/u.js:9:15)
    at WriteStream.emit (events.js:164:20)
    at finishMaybe (_stream_writable.js:616:14)
    at afterWrite (_stream_writable.js:467:3)
    at onwrite (_stream_writable.js:457:7)
    at fs.write (fs.js:2242:5)
    at FSReqWrap.wrapper [as oncomplete] (fs.js:703:5)

here is my code:

var UpdateProxyList = function(sourceURL, destinationPath) {
  var HTTP = require("http");
  var FS = require("fs");
  var File = FS.createWriteStream(destinationPath);
  HTTP.get(sourceURL, function(response) {
    response.pipe(File);
    File.on('finish', function() {
      File.close();
    });
    File.on('error', function(error) {
      FS.unlink(destinationPath);
    })
  });
}
UpdateProxyList("http://www.example.com/proxy.txt", "myproxylist.txt");

Im on MacOS Sierra with node.js v9.3.0.
apparently, when I use node.js v8.9.3, it works fine

回答1:

Between v8.9.3 and v9.3.0, the implementation of WriteStream.prototype.close has changed.

In v8.9.3, it was a reference to ReadStream.prototype.close, for which a callback argument was optional.

In v9.3.0, it is now a separate method that, amongst other things, emits a close event:

WriteStream.prototype.close = function(cb) {
  if (this._writableState.ending) {
    this.on('close', cb);
    return;
  }
  ...
};

The error that you get is caused by this.on('close', cb), which requires a Function second argument that isn't being passed in your code.

I'm not sure if you actually need to use a finish handler at all in your situation, as writable handling will be done internally by the .pipe() code.