Node is running but you don't know why? why-is-node-running is here to help you.

Overview

why-is-node-running

Node is running but you don't know why? why-is-node-running is here to help you.

Installation

Node 8 and above:

npm i why-is-node-running -g

Earlier Node versions (no longer supported):

Usage

const log = require('why-is-node-running') // should be your first require
const net = require('net')

function createServer () {
  const server = net.createServer()
  setInterval(function () {}, 1000)
  server.listen(0)
}

createServer()
createServer()

setTimeout(function () {
  log() // logs out active handles that are keeping node running
}, 100)

Save the file as example.js, then execute:

node ./example.js

Here's the output:

There are 5 handle(s) keeping the process running

# Timeout
/home/maf/dev/node_modules/why-is-node-running/example.js:6  - setInterval(function () {}, 1000)
/home/maf/dev/node_modules/why-is-node-running/example.js:10 - createServer()

# TCPSERVERWRAP
/home/maf/dev/node_modules/why-is-node-running/example.js:7  - server.listen(0)
/home/maf/dev/node_modules/why-is-node-running/example.js:10 - createServer()

# Timeout
/home/maf/dev/node_modules/why-is-node-running/example.js:6  - setInterval(function () {}, 1000)
/home/maf/dev/node_modules/why-is-node-running/example.js:11 - createServer()

# TCPSERVERWRAP
/home/maf/dev/node_modules/why-is-node-running/example.js:7  - server.listen(0)
/home/maf/dev/node_modules/why-is-node-running/example.js:11 - createServer()

# Timeout
/home/maf/dev/node_modules/why-is-node-running/example.js:13 - setTimeout(function () {

Important Note! unrefed timers do not prevent the Node process from exiting. If you are running with Node v11.0.0 and above, unrefed timers will not be listed in the above list. Unfortunately, this is not supported in node versions below v11.0.0.

CLI

You can also run why-is-node-running as a standalone if you don't want to include it inside your code. Sending SIGUSR1/SIGINFO signal to the process will produce the log. (Ctrl + T on macOS and BSD systems)

why-is-node-running /path/to/some/file.js
probing module /path/to/some/file.js
kill -SIGUSR1 31115 for logging

To trigger the log:

kill -SIGUSR1 31115

Require CLI Option

You can also use the node -r option to include why-is-node-running:

node -r why-is-node-running/include /path/to/some/file.js

The steps are otherwise the same as the above CLI section

License

MIT

Comments
  • Cannot find module 'internal/linkedlist'

    Cannot find module 'internal/linkedlist'

    Heyo, I just wanted to play around with this to see how it works. Apparently I don't get it to work. :(

    I've got a simple index.js.

    var log = require('why-is-node-running') // should be your first require
    
    log();
    

    And why-is-node-running is available installed in my node_modules.

    That's what I got:

    stefan @ stefan-mac: ~/Downloads/test
    > node --version
    v5.0.0
    
    stefan @ stefan-mac: ~/Downloads/test
    > node ./index.js
    module.js:339
        throw err;
        ^
    
    Error: Cannot find module 'internal/linkedlist'
        at Function.Module._resolveFilename (module.js:337:15)
        at Function.Module._load (module.js:287:25)
        at Module.require (module.js:366:17)
        at require (module.js:385:17)
        at timers.js:4:11
        at NativeModule.compile (/Users/stefan/Downloads/test/node_modules/why-is-node-running/core.js:108:3)
        at Function.NativeModule.require (/Users/stefan/Downloads/test/node_modules/why-is-node-running/core.js:52:16)
        at Object.globalTimeouts (/Users/stefan/Downloads/test/node_modules/why-is-node-running/core.js:14:31)
        at Object.<anonymous> (/Users/stefan/Downloads/test/node_modules/why-is-node-running/index.js:50:6)
        at Module._compile (module.js:425:26)
    FAIL: 1
    

    Thanks.

    Any hints are more than welcome. :bowtie:

    opened by stefanjudis 21
  • TypeError: self._handle.connect is not a function

    TypeError: self._handle.connect is not a function

    2016-04-07 11 22 05

    Something went wrong on net module binding when using why-is-node-running with request+bluebird combo.

    I got this error:

    net.js:822
          err = self._handle.connect(req, address, port);
                             ^
    
    TypeError: self._handle.connect is not a function
        at connect (net.js:822:26)
        at net.js:984:7
        at GetAddrInfoReqWrap.asyncCallback [as callback] (dns.js:63:16)
        at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:82:10)
    

    I tried to find the reason why it is fails, but i could't find.

    This is test code which i used.

    'use strict';
    
    /**
     * Module dependencies.
     */
    const why         = require('why-is-node-running');
    
    const
      Promise     = require('bluebird'),
      request     = require('request');
    
    
    const
      feeds = [
        'http://www.gojobs.go.kr/employ/rss.asp',
        'https://github.com/mooyoul.atom',
        'https://github.com/Flexget/Flexget/commits/develop.atom'
      ];
    
    const fetch = (url) => {
      return new Promise((resolve, reject) => {
        request({
          method: 'GET',
          url: url
        }, (e, res, body) => {
          if (e) {
            return reject(e);
          }
    
          resolve(body);
        });
      });
    };
    
    Promise.map(feeds, (feed, index) => {
      console.log('fetching feed index %d: %s', index, feed);
      return fetch(feed);
    });
    
    opened by mooyoul 9
  • use async_hooks

    use async_hooks

    Uses async_hooks to track the handles and stacks. Much, much cleaner but only supported by node >= 8 AFAIK (cc @andreas_madsen).

    Thoughts @ralphtheninja @yoshuawuyts ?

    opened by mafintosh 7
  • Weird behaviour with stack-chain

    Weird behaviour with stack-chain

    require('why-is-node-running')
    require('stack-chain')
    
    console.log(process) // Accessing process!
    
    > node index.js
    /tmp/node_modules/stackback/index.js:33
        err.stack;
           ^
    
    TypeError: Cannot redefine property: _sb_callsites
    
    opened by julien-f 3
  • Requirements and impact

    Requirements and impact

    Is there a minimal Node version requirement for this module?

    Is there any performance impact due to the use of this module?

    Should it be used in production?

    opened by julien-f 3
  • Lots of RANDOMBYTESREQUEST handles of crypto since ~ node 15

    Lots of RANDOMBYTESREQUEST handles of crypto since ~ node 15

    I used why-is-not-running in node 12 with much satisfaction but now I've upgraded to the node 16 LTS it is not working anymore for my use-case.

    The native Node crypto module now gives a lot of RANDOMBYTESREQUEST false positives.

    I think this is related to an issue on Jest: https://github.com/facebook/jest/issues/11275. Their fix: https://github.com/facebook/jest/pull/11278/files

    opened by SanderDeWaal1992 2
  • Don't report unfulfilled promises

    Don't report unfulfilled promises

    Unfulfilled promises don't prevent node from stopping. In my case, the result was thousands of lines of spurious output, making it impossible to find the one setTimeout that was causing the problem.

    opened by tbodt 2
  • This could lead to memory leak of Promise/PromiseWrap

    This could lead to memory leak of Promise/PromiseWrap

    Hello guys,

    I was using this module to track what is pending in my nodejs project, and once my node process ended with heap out of memory, I was confused a lot by checking my code for memory leak...

    Finally with ChromeDev tools, I found that the objects Array, Promise & PromiseWrap were always increasing without being released by GC, and they're somehow related to this module.

    By checking the source code, async-hook was used to track all the Promises/async/await, and they're put into a map... so I think that's where the leak happens. and after removed require('why-is-node-running') from my code, the memory leak was gone.

    So maybe I would like to recommend to put some WARNINGS in the README.md file of this module like: if you're creating new Promises to wrap traditional callbacks a lot, this module could lead to memory leaks...

    Thanks.

    opened by strawhatboy 2
  • `filename` is null, cannot read .indexOf of null

    `filename` is null, cannot read .indexOf of null

    Change this line:

    return filename.indexOf(sep) > -1 && filename.indexOf('internal' + sep) !== 0

    to

    return filename && filename.indexOf(sep) > -1 && filename.indexOf('internal' + sep) !== 0

    Somehow the filename is null on my Node 8.9

    opened by anton-bot 2
  • Usage with mocha

    Usage with mocha

    I am trying to use this tool to figure out why my mocha tests are hanging, as recommended.

    Our project has the following script defined in package.json:

        "test": "NODE_ENV=test NODE_PATH=./src:./test mocha src/**/__tests__/**/*.js",
    

    We typically use the command yarn test to run our tests.

    What command should I use to run our tests under this tool? When I do this:

    $ why-is-node-running yarn test

    I get:

    Error: Cannot find module '/Users/mpelzsherman/source/ag.js/yarn'
    

    Many thanks!

    opened by mpelzsherman 2
  • This repo may be mis-reporting histogram references

    This repo may be mis-reporting histogram references

    This repo appears to be mis-reporting histogram.enable() as a lingering reference:

    Related to https://github.com/siimon/prom-client/issues/494

    # ELDHISTOGRAM
    node:internal/async_hooks:201                                                   
    node:internal/perf/event_loop_delay:81                                          
    node:internal/perf/event_loop_delay:78                                          
    /var/app/example/node_modules/prom-client/lib/metrics/eventLoopLag.js:47 - const histogram = perf_hooks.monitorEventLoopDelay({
    /var/app/example/node_modules/prom-client/lib/defaultMetrics.js:43       - metric(config.register, config);
    

    This was reported upstream to nodejs https://github.com/nodejs/node/issues/42132 and they concluded histograms should not be showing here since they are unref'd at the runtime level. Therefore we suspect that it may be a false positive here in this repo.

    I see that you have a number of other types blocklisted here: https://github.com/mafintosh/why-is-node-running/blob/24fb4c878753390a05d00959e6173d0d3c31fddd/index.js#L8-L11

    I'm wondering if histogram's should be added here as well?

    opened by justinmchase 2
  • Source Map line numbers are wrong with mocha and ts-node

    Source Map line numbers are wrong with mocha and ts-node

    So as the title says, when I run this package with mocha like so: why-is-node-running node_modules/.bin/_mocha --inspect -r ts-node/register 'src/**/*.test.ts' or so: mocha --inspect -r ts-node/register -r why-is-node-running/include 'src/**/*.test.ts' the output points to the wrong line in the ts source files or even the wrong file completely.

    opened by Dzeri96 1
  • Support for use directly through command line with `npx`

    Support for use directly through command line with `npx`

    It would be really nice if this tool offered a CLI interface that can be used with npx why-is-node-running. Any plans of adding that interface @mafintosh ? I can help!

    opened by rbelling 2
  • In Windows 10 the CLI version returns unknown PID

    In Windows 10 the CLI version returns unknown PID

    After running from CLI and getting this PID:

    kill -SIGUSR1 11020 for logging
    

    We can't find that PID in the active process:

    $ ps
          PID    PPID    PGID     WINPID   TTY         UID    STIME COMMAND
        14264   13280   13280      15272  cons0    1049692 18:36:57 /c/Program Files/nodejs/node
        13280   11132   13280      15860  cons0    1049692 18:36:57 /usr/bin/sh
        11132       1   11132      11132  cons0    1049692 17:34:12 /usr/bin/bash
    
    $ bash: kill: (11020) - No such process
    

    Trying with the active PIDs we get some messages but not the log:

    $ kill -SIGUSR1 14264
          1 [sig] sh 14264! sigpacket::process: Suppressing signal 30 to win32 process (pid 15272)
    
    $ kill -SIGUSR1 13280
          User defined signal 1
    
    

    Any idea?

    Thanks

    opened by IGZmanuelMartinVivaldi 1
  • ES modules support?

    ES modules support?

    Running why-is-node-running myscript.mjs produces

    > internal/modules/cjs/loader.js:1007
      throw new ERR_REQUIRE_ESM(filename);
      ^
    
    Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: myscript.mjs
    

    ES Modules support has just landed.

    opened by dandv 1
Owner
Mathias Buus
Rød grød med fløde
Mathias Buus
Well-formatted and improved trace system calls and signals (when the debugger does not help)

ctrace Well-formatted and improved trace system calls and signals (when the debugger does not help). Why? Awesome tools strace and dtruss have only on

Aleksandr Komlev 117 Dec 27, 2022
A tiny JavaScript debugging utility modelled after Node.js core's debugging technique. Works in Node.js and web browsers

debug A tiny JavaScript debugging utility modelled after Node.js core's debugging technique. Works in Node.js and web browsers. Installation $ npm ins

Sloth 10.5k Dec 30, 2022
An lldb plugin for Node.js and V8, which enables inspection of JavaScript states for insights into Node.js processes and their core dumps.

Node.js v10.x+ C++ plugin for the LLDB debugger. The llnode plugin adds the ability to inspect JavaScript stack frames, objects, source code and more

Node.js 1k Dec 14, 2022
Node.js debugger based on Blink Developer Tools

Node Inspector Overview Node Inspector is a debugger interface for Node.js applications that uses the Blink Developer Tools (formerly WebKit Web Inspe

null 12.6k Dec 29, 2022
ndb is an improved debugging experience for Node.js, enabled by Chrome DevTools

ndb ndb is an improved debugging experience for Node.js, enabled by Chrome DevTools Installation Compatibility: ndb requires Node >=8.0.0. It works be

null 10.8k Dec 28, 2022
[OBSOLETE] runs Node.js programs through Chromium DevTools

devtool ⚠️ Update: This tool is mostly obsolete as much of the philosophy has been brought into Node/DevTool core, see here for details. If you wish t

Jam3 3.8k Dec 20, 2022
Debug Node.js code with Chrome Developer Tools.

Debug Node.js code with Chrome Developer Tools on Linux, Windows and OS X. This software aims to make things easier ?? . With ironNode you have the fu

Stephan Ahlf 2.3k Dec 30, 2022
🐛 Memory leak testing for node.

Leakage - Memory Leak Testing for Node Write leakage tests using Mocha or another test runner of your choice. Does not only support spotting and fixin

Andy Wermke 1.6k Dec 28, 2022
Long stack traces for node.js inspired by https://github.com/tlrobinson/long-stack-traces

longjohn Long stack traces for node.js with configurable call trace length Inspiration I wrote this while trying to add long-stack-traces to my server

Matt Insler 815 Dec 23, 2022
API Observability. Trace API calls and Monitor API performance, health and usage statistics in Node.js Microservices.

swagger-stats | API Observability https://swaggerstats.io | Guide Trace API calls and Monitor API performance, health and usage statistics in Node.js

slana.tech 773 Jan 4, 2023
A Node.js tracing and instrumentation utility

njsTrace - Instrumentation and Tracing njstrace lets you easily instrument and trace you code, see all function calls, arguments, return values, as we

Yuval 354 Dec 29, 2022
Locus is a debugging module for node.js

ʆ Locus Locus is a debugging module which allows you to execute commands at runtime via a REPL. Installing npm install locus --save-dev Using require(

Ali Davut 300 Nov 13, 2022
Streamline Your Node.js Debugging Workflow with Chromium (Chrome, Edge, More) DevTools.

NiM (Node.js --inspector Manager) Streamlines your development process Google Chrome Web Store (works with any Chromium browsers: Google's Chrome, Mic

Will 188 Dec 28, 2022
thetool is a CLI tool to capture different cpu, memory and other profiles for your node app in Chrome DevTools friendly format

thetool thetool is a CLI tool to capture different cpu, memory and other profiles for your node app in Chrome DevTools friendly format. Quick start np

null 200 Oct 28, 2022
Bugs bugging you? Bug back.

bugger Warning: Experimental bugger provides Chrome Devtools bindings for node. It integrates tightly with Chrome which means two things: It attempts

bugger 157 Aug 8, 2022
A simple Chromium browser extension to every so often Rickroll yourself. Every link you click has a 1% chance of being a Rickroll.

Rick Rollette A simple Chromium browser extension to every so often Rickroll yourself. Every link you click has a 1% chance of being a Rickroll. How t

Davi Augusto Moreira da Silva 43 Jun 9, 2022
ReCord - really i dont even know, start obs recording by joining voice channel

ReCord starts recording Voice Calls in Discord How To Use Requirements: NodeJS A text editor OBS webhook plugin Step 1: create a config.json file Ste

cyan 1 May 1, 2022
WhatsApp Bot With Baileys, Dont forget star & fork

Rhapsody-Bot > pkg update && pkg upgrade > pkg install nodejs yarn ffmpeg imagemagick libwebp sox > git clone https://github.com/DEFF-Y/Rhapsody-Bot

Daffa Yudhistira 6 Nov 15, 2022
JSPro is nothing but JavaScript Prototypes! The publisher is too lazy to write full name that's why it's just JSPro.

JSPro is nothing but JavaScript Prototypes! The publisher is too lazy to write full name that's why it's just JSPro. Whatever, it's a library of hundreds of awesome JavaScript Prototypes (you may know it as dot function) for lazy programmers. Just install the package with a little effort and leave the blames for the publisher.

Jafran Hasan 2 Mar 10, 2022
e-ONG, an authorial project, whose objective is to help ONGs to find people who need help or would like to help them

This project was bootstrapped with Create React App. Available Scripts In the project directory, you can run: npm start Runs the app in the developmen

Lucas Lima 2 Nov 11, 2022