javascript - Node: required module not in scope of function inside module.exports? -
i'm having scoping issue seem unable elegantly solve.
scenario
the top of file has module required in next way
var moment = require('moment-timezone');
what attempting do, take function saved json string , convert function (using json-fn). below require have like:
module.exports = function() { //job[1] json hash holding function. more on in below text. var func = jsonfn.parse(job[1]) func() };
in case of function has no need of file's modules, can invoke function , works perfectly. however, when seek invoke function using moment module, below error:
error referenceerror: moment not defined @ eval
despite best efforts, cannot seem 'moment' variable function's scope. works if alter module's require statement to:
global.moment = require('moment-timezone');
i have tried multiple different modules, confirm wasn't specific moment, , no module have tested has worked.
question
is scoping module global object appropriate way prepare problem? or there other method of attaching function right scope?
thanks in advance help , looking forwards reading suggestions!
javascript node.js module scope eval
No comments:
Post a Comment