javascriptnode.jslightstreamer

Node.js script hangs on require, but works fine in the REPL


I'm running node.js on Ubuntu 18.04 LTS. I appear to be getting different behaviour using require depending on whether I use it in the REPL or in a script. Specifically, I used npm to download lightstreamer-client-node. Now, I open up a terminal and do the following:

colin@colin-XPS-15-9550:~$ node
> var x = require('lightstreamer-client-node')

This works perfectly.

Now I want to use this package in a script. I create a text file node_test.js containing just the line:

var x = require('lightstreamer-client-node')

and I open up a terminal and run the command:

colin@colin-XPS-15-9550:~$ node /home/colin/node_test.js

This hangs indefinitely on a blinking cursor.

I'm brand new to node.js and JavaScript so perhaps this is expected behaviour. I've done some reading about the require function and can't seem to find an explanation for it. Note that if I replace lightstreamer-client-node with some other node module, e.g. safe-buffer, then everything works fine, whether I use REPL or script.


Solution

  • The process seems to hang because the library lightstreamer-client installs a timer, with the function setInterval, for its internal activities, and nodejs doesn't allow a graceful shutdown when there are active tasks. So the only way to terminate the script is by using the function process.exit.