Node 红色功能 Node 的第一行是
var moment = require('moment-timezone');
...
我正在尝试为传感器数据建立时区正确的日期/时间戳。此 Node 运行时出现以下错误;
ReferenceError: require is not defined (line 1, col 14)
顺便说一下,这个函数有其他 JavaScript 总是完美运行。
我的 Package.json 没有错误,我添加了“moment-timezone”:“0.5.3”。
我从一点点或研究中了解到我需要向 settings.js 文件添加一些内容,但是,我需要一些关于添加内容的指导,以便识别“require”。
最佳答案
作为this GitHub issue answer声明,您不能在函数 Node 内使用 require
本身,但您可以将外部模块添加到用于运行函数的沙箱中。您可以在 settings.js
文件中执行此设置 functionGlobalContext
,如下所示:
functionGlobalContext: {
tzModule:require('moment-timezone')
}
然后可以使用以下代码引用该模块:
var moment = global.get('tzModule');
查看 Node-RED documentation on global-context了解全部详情。
关于javascript - “require”关键字在 Node Red Function Node 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36756858/