Note that there are some explanatory texts on larger screens.

plurals
  1. PONodeJS vm.runInThisContext() and lack of __filename
    primarykey
    data
    text
    <p>Whenever I run <a href="http://nodejs.org/docs/v0.4.8/api/vm.html#vm.runInThisContext" rel="nofollow"><code>vm.runInThisContext(code, filename)</code></a>, the code I ran reports <a href="http://nodejs.org/docs/v0.4.8/api/globals.html#__filename" rel="nofollow"><code>__filename</code></a> and <a href="http://nodejs.org/docs/v0.4.8/api/globals.html#__dirname" rel="nofollow"><code>__dirname</code></a> as <code>undefined</code>.</p> <p>This also leads to the situation that any <code>fs.readFile</code> and such calls will not work with relative paths. Actually to be exact, file system functions do not work at all even if I feed them a hard-coded absolute path to an existing file.</p> <p>For example, this will do nothing:</p> <pre><code>var fs = require('fs'); fs.readFile('/home/test/file.txt', function(e, data) { if (e) {throw e;} console.log('here i am'); }); </code></pre> <p>What happens is that nothing happens. If I run the code in normal NodeJS code then it outputs "here i am", but if I run that code through the <code>vm</code> module, then nothing happens. The callback is simply never called, because for some reason it can't locate the file and there does not seem to be any timeouts either.</p> <p>How can I make Node to understand that the executed code is some "file" and also make the <code>fs</code> module functions to work? I tried specifying the second parameter to <a href="http://nodejs.org/docs/v0.4.8/api/vm.html#vm.runInThisContext" rel="nofollow"><code>vm.runInThisContext(code, filename)</code></a>, but I see no difference. It almost looks that Node doesn't care about the second parameter.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload