问题描述
var moment = require('moment-timezone');
var moment = require('moment-timezone');
我正在尝试为传感器数据建立时区正确的日期/时间戳。此节点运行时出现以下错误;
I am trying to establish a timezone correct date/time stamp for sensor data. I get the following error when this node runs;
ReferenceError:require未定义(第1行,第14行)
顺便说一句,这个函数还有其他一直运行良好的JavaScript。
This function, by the way, has other JavaScript which always runs perfectly.
我的Package.json没有错误,我有,moment-timezone:0.5.3补充。
My Package.json has no errors and I have the, "moment-timezone":"0.5.3" added.
我从一点或研究中了解到我需要在settings.js文件中添加一些东西,但是,我需要一些关于添加内容的指导,以便识别'require'。
I understand from a bit or research that I need to add something to the settings.js file, however, I need some guidance on what to add so that 'require' is recognized.
推荐答案
as 说明,你不能使用 require
本身在函数节点内,但您可以将外部模块添加到用于运行函数的沙箱中。您可以在 settings.js
文件中执行此设置 functionGlobalContext
,如下所示:
As this GitHub issue answer states, you cannot use require
itself inside a function node, but you can add external modules to the sandbox used to run functions. You would do this setting functionGlobalContext
inside your settings.js
file like the following:
functionGlobalContext: {
tzModule:require('moment-timezone')
}
然后可以使用以下代码引用该模块:
The module can then be referenced by using the following code:
var moment = global.get('tzModule');
查看以获取完整的详细信息。
Check out the Node-RED documentation on global-context for full details.
这篇关于'require'关键字在节点红色功能节点中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!