Node.js TypeError: Invalid non-string/buffer chunk

ANSWER:
I’ve finally got it, turns out I was also looking in the wrong place for the logger file. it places it in the main folder instead of the folder the javascript was in. The code that worked for me was:

var fs = require('fs');
var log = fs.createWriteStream('nodelogger.txt', {flags: 'a', encoding: 'utf-8',mode: 0666});

server.listen(8888);

app.use("/", express.static(__dirname + '/files'));


io.sockets.on('connection', function (socket) {
  socket.on('msg', function (data) {
    io.sockets.emit('new', data);
    log.on('error', function (err) {
      console.log(err);
    });

    console.log(data);
    log.on('error', function(e) { console.error(e); });
    var newdata = JSON.stringify(data);
    log.write(newdata += "\r\n")
  });
});

I’m totally new to Node.js so I could really use some help.
I’m trying to log messages from the chat I’m making
So far I have this:

var fs = require('fs');
var log = fs.createWriteStream('nodelogger.txt', {'flags': 'a'});

server.listen(8888);

app.use("/", express.static(__dirname + '/files'));


io.sockets.on('connection', function (socket) {
  socket.on('msg', function (data) {
    io.sockets.emit('new', data);
    log.write(data);
    log.on('error', function (err) {
      console.log(err);
    });
  });
});

But I keep getting this error:

events.js:72
        throw er; // Unhandled 'error' event
              ^
TypeError: Invalid non-string/buffer chunk
    at validChunk (_stream_writable.js:150:14)
    at WriteStream.Writable.write (_stream_writable.js:179:12)
    at Socket.<anonymous> (/Users/test/Desktop/Chat/app.js:16:9)
    at Socket.EventEmitter.emit [as $emit] (events.js:95:17)
    at SocketNamespace.handlePacket (/Users/test/Desktop/Chat/node_modules/socket.io/lib/namespace.js:335:22)
    at Manager.onClientMessage (/Users/test/Desktop/Chat/node_modules/socket.io/lib/manager.js:488:38)
    at WebSocket.Transport.onMessage (/Users/test/Desktop/Chat/node_modules/socket.io/lib/transport.js:387:20)
    at Parser.<anonymous> (/Users/test/Desktop/Chat/node_modules/socket.io/lib/transports/websocket/hybi-16.js:39:10)
    at Parser.EventEmitter.emit (events.js:95:17)
    at finish (/Users/test/Desktop/Chat/node_modules/socket.io/lib/transports/websocket/hybi-16.js:288:16)

And I have no idea what it means, can’t find it anywhere either. Does anybody have an idea what I’m doing wrong? Thanks in advance!

55 thoughts on “Node.js TypeError: Invalid non-string/buffer chunk”