'require' keyword doesn't work within Node Red Function node

我怕爱的太早我们不能终老 提交于 2019-12-12 08:09:14

问题


first line in a Node red Function Node is

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 is not defined (line 1, col 14)

This function, by the way, has other JavaScript which always runs perfectly.

My Package.json has no errors and I have the, "moment-timezone":"0.5.3" added.

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.


回答1:


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.




回答2:


Settings file:/Users/aiquantong/.node-red/settings

Please do configure in this file, it will be working.



来源:https://stackoverflow.com/questions/36756858/require-keyword-doesnt-work-within-node-red-function-node

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!