2011-03-10 21:30

如何在 Node.js 中退出


What is the command that is used to exit? (i.e terminate the Node.js process)


  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答


  • weixin_41568183 零零乙 7年前

    Call the global process object's exit method:


    From the docs:


    Ends the process with the specified code. If omitted, exit uses the 'success' code 0.

    To exit with a 'failure' code:


    The shell that executed node should see the exit code as 1.

    点赞 25 评论 复制链接分享
  • weixin_41568208 北城已荒凉 9年前

    From the command line, .exit is what you want:

    $ node
    > .exit

    It's documented in the REPL docs. REPL (Read-Eval-Print-Loop) is what the Node command line is called.

    From a normal program, use process.exit([code]).

    点赞 28 评论 复制链接分享
  • weixin_41568183 零零乙 9年前

    If you're in a Unix terminal or Windows command line and want to exit the Node REPL, either...

    • Press Ctrl + C twice, or
    • type .exit and press Enter, or
    • press Ctrl + D at the start of a line (Unix only)
    点赞 27 评论 复制链接分享
  • csdnceshi70 笑故挽风 10年前

    From the official nodejs.org documentation:


    Ends the process with the specified code. If omitted, exit uses the 'success' code 0.

    To exit with a 'failure' code:

    点赞 26 评论 复制链接分享
  • csdnceshi68 local-host 5年前

    Just a note that using process.exit([number]) is not recommended practice.

    The reason this is problematic is because writes to process.stdout in Node.js are non-blocking and may occur over multiple ticks of the Node.js event loop. Calling process.exit(), however, forces the process to exit before those additional writes to stdout can be performed.

    Rather than calling process.exit() directly, the code should set the process.exitCode and allow the process to exit naturally by avoiding scheduling any additional work for the event loop:

    If you can you should set the exit code and allow Node to exit gracefully:

    process.exitCode = 1;
    点赞 23 评论 复制链接分享
  • csdnceshi53 Lotus@ 5年前

    To exit

    let exitCode = 1;

    Useful exit codes

    1 - Catchall for general errors
    2 - Misuse of shell builtins (according to Bash documentation)
    126 - Command invoked cannot execute
    127 - “command not found”
    128 - Invalid argument to exit
    128+n - Fatal error signal “n”
    130 - Script terminated by Control-C
    255\* - Exit status out of range
    点赞 10 评论 复制链接分享
  • csdnceshi56 lrony* 5年前

    It depends on the reason why you're willing to exit node.js process, but in any case process.exit() is the last option to consider. A quote from documentation:

    It is important to note that calling process.exit() will force the process to exit as quickly as possible even if there are still asynchronous operations pending that have not yet completed fully, including I/O operations to process.stdout and process.stderr.

    In most situations, it is not actually necessary to call process.exit() explicitly. The Node.js process will exit on it's own if there is no additional work pending in the event loop. The process.exitCode property can be set to tell the process which exit code to use when the process exits gracefully.

    Let’s cover possible reasons why you might be willing to exit node.js process and why you should avoid process.exit():

    Case 1 - Execution complete (command line script)

    If script has reached its end and node interpreter doesn't exit, it indicates that some async operations are still pending. It’s wrong to force process termination with process.exit() at this point. It’s better to try to understand what is holding your script from exiting in expected way. And when you settle this, you can use process.exitCode to return any result to calling process.

    Case 2 - Termination because of external signal (SIGINT/SIGTERM/other)

    For example, if you’re willing to gracefully shut down an express app. Unlike command line script, express app keeps running infinitely, waiting for new requests. process.exit() will be a bad option here because it’s going to interrupt all requests which are in pipeline. And some of them might be non-idempotent (UPDATE, DELETE). Client will never know if those requests are completed or not on server side and it might be the reason of data inconsistency between client and server. The only good solution is to tell http server to stop accepting new requests and wait for pending ones to finish with server.close():

    var express = require('express');
    var app = express();
    var server = app.listen(80);
    process.on( 'SIGTERM', function () {
       server.close(function () {
         console.log("Finished all requests");

    If it still doesn't exit - see Case 1.

    Case 3 - Internal error

    It's always better to throw an error, you’ll get a nicely formatted stack trace and error message. Upper levels of code can always decide if they can handle error (catch) or let it crash the process. On the other side, process.exit(1) will terminate process silently and there will be no chance to recover from this. It might be the only “benefit” of process.exit(), you can be sure that process will be terminated.

    点赞 9 评论 复制链接分享
  • weixin_41568208 北城已荒凉 4年前

    REPL(Command Line)

    • Press ctrl + c twice

    • Type .exit and press enter

    Script File


    Node normally exits with code 0 when no more async operations are pending.

    process.exit(1) should be used to exit with a failure code.This will allow us to infer that node didn't close gracefully and was forced to close.

    There are other exit codes like

    3 - Internal JavaScript Parse Error ( very very rare)

    5 - Fatal error in v8 javascript engine

    9 - Invalid argument

    For full list see node exit codes

    点赞 6 评论 复制链接分享
  • csdnceshi59 ℙℕℤℝ 3年前

    Command Line

    Press CTRL + C twice


    Type .exit and press enter

    Script File

    点赞 5 评论 复制链接分享
  • csdnceshi69 YaoRaoLov 8年前

    From code you can use process.exit([errorcode]) where [errorcode] is an optional integer (0 is the default to indicate success).

    If you're using the Read Eval Print Loop (REPL), you can use Ctrl + D, or type .exit

    Alternatively, on Windows or Linux you can use Ctrl + C, Ctrl + C

    On Mac the command is Ctrl + Z, Ctrl + Z

    点赞 5 评论 复制链接分享
  • csdnceshi60 ℡Wang Yan 6年前

    I have an application which I wanted to:

    1. Send an email to the user
    2. Exit with an error code

    I had to hook process.exit(code) to an exit event handler, or else the mail will not be sent since calling process.exit(code) directly kills asynchronous events.

    var mailer = require('nodemailer');
    var transport = mailer.createTransport();
    mail = {
      to: 'Dave Bowman',
      from: 'HAL 9000',
      subject: 'Sorry Dave',
      html: 'Im sorry, Dave. Im afraid I cant do <B>THAT</B>.'
    process.on('exit', function() { process.exit(1); });
    点赞 4 评论 复制链接分享
  • csdnceshi67 bug^君 3年前

    Press Ctrl + C twice or .exit.

    (To exit, press ^C again or type .exit)
    点赞 4 评论 复制链接分享
  • weixin_41568134 MAO-EYE 4年前

    I was able to get all my node processes to die directly from the Git Bash shell on Windows 10 by typing taskkill -F -IM node.exe - this ends all the node processes on my computer at once. I found I could also use taskkill //F //IM node.exe. Not sure why both - and // work in this context. Hope this helps!

    点赞 3 评论 复制链接分享
  • csdnceshi79 python小菜 5年前

    As @Dominic pointed out, throwing an uncaught error is better practice instead of calling process.exit([code]):
    process.exitCode = 1; throw new Error("my module xx condition failed");

    点赞 1 评论 复制链接分享
  • csdnceshi56 lrony* 3年前

    You may use process.exit([code]) function.

    If you want to exit without a 'failure', you use code 0:


    To exit with a 'failure' code 1 you may run:


    The 'failure' code of the failure is specific to the application. So you may use your own conventions for it.

    点赞 评论 复制链接分享
  • csdnceshi78 程序go 4年前

    if you want to exit from node js application then write


    in your code

    点赞 评论 复制链接分享