简体   繁体   English

Node.js 脚本挂在要求上,但在 REPL 中工作正常

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

I'm running node.js on Ubuntu 18.04 LTS.我在 Ubuntu 18.04 LTS 上运行node.js I appear to be getting different behaviour using require depending on whether I use it in the REPL or in a script.根据我是在 REPL 中还是在脚本中使用require ,我似乎得到了不同的行为。 Specifically, I used npm to download lightstreamer-client-node .具体来说,我使用npm来下载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.现在我想在脚本中使用这个 package。 I create a text file node_test.js containing just the line:我创建了一个文本文件node_test.js仅包含以下行:

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.这在闪烁的 cursor 上无限期挂起。

I'm brand new to node.js and JavaScript so perhaps this is expected behaviour.我是node.jsJavaScript的新手,所以这可能是预期的行为。 I've done some reading about the require function and can't seem to find an explanation for it.我已经阅读了一些有关require function 的内容,但似乎找不到解释。 Note that if I replace lightstreamer-client-node with some other node module, eg safe-buffer , then everything works fine, whether I use REPL or script.请注意,如果我将lightstreamer-client-node替换为其他一些节点模块,例如safe-buffer ,那么无论我使用 REPL 还是脚本,一切正常。

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.该过程似乎挂起,因为库 lightstreamer-client 安装了一个计时器,其中 function setInterval 用于其内部活动,并且 nodejs 不允许在有活动任务时正常关闭。 So the only way to terminate the script is by using the function process.exit.所以终止脚本的唯一方法是使用 function process.exit。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM