[英]NodeJS module executing user script with external function not found
I am quite new to nodeJS. 我对nodeJS很陌生。
I am using the nodeJS module node-workflow 我正在使用nodeJS模块node-workflow
Basically, this module is an orchestrator that takes a custom javascript script (=workflow definition), then serialize it and store it in a REDIS db (for example), and execute on-demand later on by the node-workflow module. 基本上,此模块是一个协调器,它采用自定义javascript脚本(=工作流定义),然后对其进行序列化并将其存储在REDIS db中(例如),然后由节点工作流模块按需执行。
A workflow definition is composed of task, like this: 工作流定义由任务组成,如下所示:
var my_external_module = require('my_external_module');
var workflow = module.exports = {
name: 'Workflow Test',
chain: [{
name: 'TASK 1',
timeout: 30,
retry: 1,
body: function(job, cb) {
// Execute external function
my_external_module.hello("Monkey");
return cb(null)
},
},
...
First I put my function my_external_module.hello() in a .js file beside the workflow script. 首先,我将我的函数my_external_module.hello()放在工作流脚本旁边的.js文件中。 When I run the node-workflow module I get the following error:
当我运行节点工作流模块时,出现以下错误:
Error initializing runner:
初始化跑步者时出错:
[ReferenceError: my_external_module is not defined]
[ReferenceError:未定义my_external_module]
So I have created a module my_external_module , and in: ./node_modules/my_external_module/index.js 因此,我在以下位置创建了模块my_external_module : ./node_modules/my_external_module/index.js
module.exports = {
hello: function(name) {
console.log("Hello, " + name);
}
};
When I run the node-workflow module I get the same error: 当我运行节点工作流模块时,出现相同的错误:
Error initializing runner:
初始化跑步者时出错:
[ReferenceError: my_external_module is not defined]
[ReferenceError:未定义my_external_module]
It seems that the require(...) shall stands in one of the .js files of the node-workflow module, so I would have to hack one of the files of the module, but it is a bit dirty. 看来require(...)应该位于node-workflow模块的.js文件之一中,所以我将不得不破解该模块的文件之一,但它有点脏。
Is there something I missed? 有什么我想念的吗?
Or is there a way to define a $PATH like in Python in order to my function to be accessible from everywhere. 还是有一种方法可以像在Python中那样定义$ PATH,以便可以从任何地方访问我的函数。
You have to require it as: 您必须将其要求为:
var my_external_module = require('./my_external_module');
Notice the ./
this means Node should search for a file like ./my_external_module.js
注意
./
这意味着Node应该搜索./my_external_module.js
类的文件。
If you omit the ./
Node looks at the installed modules on (usually) node_modules
directory 如果省略
./
节点,则通常在node_modules
目录中查看已安装的模块
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.