On Wed, Sep 25, 2013 at 11:30 AM, Ashish Negi <thisismyidash...@gmail.com> wrote: > I am just trying to run a JS code in a function. for now i think that i do > not need MakeCallback. > > I send in handle->data this js code: > var fs = require('fs'); > fs.readFile('C:\\abc\\hello.txt', function(err,data){ > if(err) > console.log("Error reading file: " + err); > else > console.log("Data is: " + data); > }); > > This is function that i gets the call: > > void RunJavascriptUv(uv_async_t *handle, int status) { > // hope that this is being called on nodejs main loop. > v8::HandleScope handleScope; > // unique_ptr makes sure that data would be deleted even if there is an > exception. > std::unique_ptr<char[]> codeData((char*)handle->data); > v8::Local<v8::Context> currContext = v8::Context::GetCurrent(); > v8::Local<v8::String> source = v8::String::New(codeData.get()); > v8::TryCatch try_catch; > v8::Local<v8::Script> script = v8::Script::Compile(source); > if (script.IsEmpty()) { > // Print errors that happened during compilation. > ReportException(NULL, &try_catch); > } else { > v8::Local<v8::Value> result = script->Run(); > if (result.IsEmpty()) { > assert(try_catch.HasCaught()); > // Print errors that happened during execution. > ReportException(NULL, &try_catch); > } else { > assert(!try_catch.HasCaught()); > if (!result->IsUndefined()) { > // If all went well and the result wasn't undefined then print > // the returned value. > v8::String::Utf8Value str(result); > const char* cstr = ToCString(str); > printf("%s\n", cstr); > } > } > } > } > > And i am getting this error: > > undefined:1: ReferenceError: require is not defined > var fs = require('fs'); > fs.readFile('C:\CEF\hello.txt', function(err > ^ > ReferenceError: require is not defined > at <anonymous>:1:10
That's because require() is not a global function, see lib/module.js for details. -- -- Job Board: http://jobs.nodejs.org/ Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines You received this message because you are subscribed to the Google Groups "nodejs" group. To post to this group, send email to nodejs@googlegroups.com To unsubscribe from this group, send email to nodejs+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/nodejs?hl=en?hl=en --- You received this message because you are subscribed to the Google Groups "nodejs" group. To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.