'require' is not defined in console tab of node-inspector's repl

末鹿安然 提交于 2021-02-07 14:26:38

问题


I tried to

require('fs').writeFileSync('o.json', JSON.stringify(anObject));

for debugging, and I got RefrenceError: require is not defined.

This is bizarre. What kind of an environment is it that doesn't have require?


回答1:


In Node.js, the require function is not defined globally. When you require a module, its source code is wrapped by a function that takes several arguments and require is one of them:

(function (exports, require, module, __filename, __dirname) {/* your code */
});

You can see that wrapper in Node Inspector when you look at the content of any source file.

Now there are two different scenarios for evaluating code from Node Inspector's console:

  1. The debugged process is running. Expressions are evaluated in the global context, i.e. you have access only to globally defined symbols. require is not among them.

  2. The debugged process is paused on a break point. The expression is evaluated in the context of the select stack frame and have access too all variables in the current scope (which include variables captured by closures from by outer functions). However: most functions don't call require, therefore this variable is not captured in any closure and you can't access it.

I had a short chat with one of the Node core developers about exposing require as a global symbol, because it would simplify injecting code from Node Inspector, but my arguments were not convincing enough.




回答2:


It is possible to use require in node-inspector, by hitting a breakpoint where 'require' is in scope (Tested with NodeJS v0.10.25, node-inspector@0.7.0-2):

An example (a super simple 2-byte 'REPL' for Node): create a file (for example r.js) containing just:

0;

then use

$ node --debug-brk r.js

and start node-inspector and the browser in the usual way,

-or- (using node-debug module from NPM) use:

$ node-debug r.js

Now switch to the node-inspector console in the browser and verify that require is indeed defined, then do something with it, like:

> typeof require;
"function"
> fs = require('fs');
undefined
> fs.writeFileSync('bla.txt', 'blup');
undefined

and verify that this file gets written in the directory where you started node.

Note: the browser part of node-inspector can be quite glitchy: when debuggong different scripts one after the other, I often need to refresh (causing a debugger disconnect), then restart Node until the script shows up correctly in the browser.

P.S. This method has an uncanny similarity to creating a Perl 'REPL' by running it as perl -de '0;'... Coincidence?




回答3:


FWIW, just by starting with node-debug _mocha, stopping at a breakpoint and selecting the lowest frame on the call stack in the right-side panel, I got to a context where require() was available: require('fs') worked OK, but require('q') and other installed modules didn't, even if I specified an absolute path. Will get back if I come across the solution.




回答4:


require() is implemented in all CommonJS environments, which include Node.js, but not in the browser's console, which is what node-inspector is based on (to be specific, the Blink Developer Tools):

> require
ReferenceError: require is not defined


来源:https://stackoverflow.com/questions/20283516/require-is-not-defined-in-console-tab-of-node-inspectors-repl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!