question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Error: write after end

See original GitHub issue

Node version: 8.11.3 OS: 64 bit Windows 10

I am using rotating-file-stream and morgan in my application to log all HTTP requests into daily rotated file. I have followed closely with the way that the docs of morgan recommended to use RFS: https://github.com/expressjs/morgan#log-file-rotation

Code: app.js

// define the log directory
let logDirectory = path.join(__dirname, 'log');

// ensure log directory exists
fs.existsSync(logDirectory) || fs.mkdirSync(logDirectory);

// create a rotating write stream
let writeStream = rfs((time) => {
    if (!time) {
        return 'access.log';
    }
    let t = moment(time);
    return `${t.format('MM-DD-YYYY')}.log`;
}, {
    interval: '1d', // daily rotate
    path: logDirectory,
    rotationTime: false,
});


// set the logger, logging every request to the console
app.use(morgan('common', {
    stream: writeStream,
}));

Error message:

events.js:183
      throw er; // Unhandled 'error' event
      ^

Error: write after end
    at writeAfterEnd (_stream_writable.js:236:12)
    at RotatingFileStream.Writable.write (_stream_writable.js:287:5)
    at Array.logRequest (C:\Haoyang\source_code\myapp\server\node_modules\morgan\index.js:130:14)
    at listener (C:\Haoyang\source_code\myapp\server\node_modules\on-finished\index.js:169:15)
    at onFinish (C:\Haoyang\source_code\myapp\server\node_modules\on-finished\index.js:100:5)
    at callback (C:\Haoyang\source_code\myapp\server\node_modules\ee-first\index.js:55:10)
    at ServerResponse.onevent (C:\Haoyang\source_code\myapp\server\node_modules\ee-first\index.js:93:5)
    at emitNone (events.js:111:20)
    at ServerResponse.emit (events.js:208:7)
    at onFinish (_http_outgoing.js:720:10)

My goal is quite simple: I just want to let morgan direct all its log into the rotating write stream created via RFS, with filename to be in MM-DD-YYYY.log format for each day. I do a check to return a fixed file name access.log for the case when the time parameter is null. However, the error still persists

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
ArtixZcommented, Oct 26, 2018

This config seems work for me by adding immutable: true,:

const logStream = rfs(generator, {
  interval: '1m', // rotate daily
  path: path.resolve(logDirectory),
  immutable: true,
})

@bryandbor : I believe rotationTime: true means time parameter will be fixed to the moment that you call the function( run the server up ).

1reaction
icciccicommented, Jul 27, 2018

Hi @HaoyangFan96,

plz try to add following code just after let writeStream = ... statement:

writeStream.on("error", console.log);
writeStream.on("warning", console.log);

The first error should help you finding the root cause of your problem.

Hope this helps, iCC

Read more comments on GitHub >

github_iconTop Results From Across the Web

Write After End error in node.js webserver - Stack Overflow
I am struggling with my node.js hobby project due to a "write after end" Error. I have a created a node.js webserver that...
Read more >
write after end · Issue #928 · tulios/kafkajs - Connection error
After the update, we noticed that a new error is logged in our production server: Message: Connection error: write after end Stack: Error ......
Read more >
Server crashes with even simplest code. : r/node - Reddit
this is error events.js:352 throw er; // Unhandled 'error' event Error [ERR_STREAM_WRITE_AFTER_END]: write after end at writeAfterEnd…
Read more >
Error: write after end (Example) | Treehouse Community
Node is asynchronous, which means that functions happen at the same time. Write after end means that something is being written after the...
Read more >
Object Storage upload: [ERR_STREAM_WRITE_AFTER_END]
Hey guys! I use aws-sdk for NodeJS for Linode Object Storage but I get the following error `[ERR_STREAM_WRITE_AFTER_END]: write after end` when uploading ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found