I use chrome devtools to debug node.js script (node --inspect script.js
, as described e.g. in
https://nodejs.org/en/docs/guides/debugging-getting-started/)
For some reason a function statement is not working properly. This is the code:
function f(){};
f=1;
console.log(f);
http = require('http');
myserver=http.createServer(function (req, res) {
res.end();
}).listen(8080);
The console outputs 1
, but then when I try to enter f
in the console it says "Uncaught ReferenceError: f is not defined".
If instead of a function statement I use function expression, everything works well:
f=function(){};
f=1;
console.log(f);
http = require('http');
myserver=http.createServer(function (req, res) {
res.end();
}).listen(8080);
So I wonder what is the source of the problem and how to fix it.
P.S. The createServer part of the script is a trick I use so that the chrome devtools console is still running after the script has been executed. By the way, is there any more straightforward way to do it?
Update:
I stumbled upon an alternative way which does not have this problem with function statement:
node --inspect -e "$(< script.js)"