2016-10-28 72 views
0

我尝试使用我的Gulpfile中另一个Javascript文件中的函数,但无法使其工作到目前为止。无法在Gulpfile中导入JavaScript文件

的文件,我需要在Gulpfile访问:

var hello = function(){ 
    console.log('Hello') 
} 

而且我需要在我的Gulpfile方式:

var tools = require('./public/js/tools.js'); 
gulp.task('create_subscriptions', function(){ 
    tools.hello(); 
}); 

tools.hello() is not a function被触发。

我在这里做错了什么?

编辑

我做

module.exports = { 
    hello: hello() 
}; 

请告诉我差机智exports.hello = hello

+1

[Node.js module.exports的用途和你如何使用它的目的是什么?](http://stackoverflow.com/questions/5311334/what-is-the-purpose-of-node -js-module-exports-how-do-you-use-it) –

+2

除非你返回一个新函数,否则你应该删除'()'。所以,'hello:hello' – Roberto14

回答

2

你没有从模块中导出任何东西。局部变量不公开,你需要将它们明确地标记为公共的。

exports.hello = hello; 

hello: hello() 

您的变量保存功能后有()。你是调用它并将返回值(这不是函数)分配给你的hello属性。

+0

非常感谢。 你可以检查我的编辑,并可能解释我吗? – Mornor