在nodejs中,如何在使用'new Function'创建的函数中使用'require'

时间:2014-05-21 03:35:09

标签: node.js require

我正在编写nodejs视图模板。 在某处我有这样的代码:

var fnStr = "var moment = require('moment'); moment(new Date);";
var fn = new Function('data', fnStr);
fn();

然后,我收到错误“ReferenceError:require is not defined”。

我检查了Node API,它说

require isn't actually a global but rather local to each module

现在我自己设定了解决方案:

global.require = require;

有更好的解决方案吗?

0 个答案:

没有答案
相关问题