Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7492437
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T16:26:21+00:00 2026-05-29T16:26:21+00:00

I have a node.js script that does some logging to a file using WriteStream.

  • 0

I have a node.js script that does some logging to a file using WriteStream. On certain events I want to stop execution of the script, i.e. warn to log and exit immediately after that. Being asyncronious node.js does not allow us to do it straight forward like:

#!/usr/local/bin/node

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

stream.write('Something bad happened\n');
process.exit(1);

Instead of appending a message to delme.log this script does nothing with the file. Handling ‘exit’ event and flushing doesn’t work. The only way to write the last log message before exitting found so far is to wrap process.exit(1) in the setTimeout():

#!/usr/local/bin/node

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

stream.write('Something bad happened\n');
setTimeout(function(){
  process.exit(1);
}, 30);

However in this form it doesn’t stop the script execution immediately and the script will be running for some time after the critical event happened. So I’m wondering if there are other ways to exit a script with a log message?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T16:26:24+00:00Added an answer on May 29, 2026 at 4:26 pm

    Since you want to block, and already are using a stream, you will probably want to handle the writing yourself.

    var data = new Buffer('Something bad happened\n');
    fs.writeSync(stream.fd, data, 0, data.length, stream.pos);
    process.exit();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have a node js script that runs a command to execute the following
I've got a node.js script that loads an XML file. It loops through each
I currently have the following JavaScript/jQuery script that gets an external html page using
I have a node with many CCK fields. I want to hide a field
In the sitemap file, I have sitemap Node URL which have more than 1
I have a binary tree where each node can have a value. I want
I want to have a tree in memory, where each node can have multiple
i have written a snake program using javascript.. the problem is that the snake
I have a large filesystem that I need to traverse for errors. Each file
I currently have node.js, express, and mongodb in my heroku environment. What I want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.