1

I try to use function from another Javascript file in my Gulpfile, but cannot make it work so far.

The file I need to access in Gulpfile:

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

And the way I require it in my Gulpfile:

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

tools.hello() is not a function is triggered.

What do I do wrong here?

Edit

I did

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

Whats the difference wit exports.hello = hello?

Mornor
  • 3,471
  • 8
  • 31
  • 69
  • 1
    Possible duplicate of [What is the purpose of Node.js module.exports and how do you use it?](http://stackoverflow.com/questions/5311334/what-is-the-purpose-of-node-js-module-exports-and-how-do-you-use-it) – Sven Schoenung Oct 28 '16 at 08:47
  • 2
    You should remove `()` unless you return a new function.. So, `hello: hello` – Roberto14 Oct 28 '16 at 08:49

1 Answers1

3

You didn't export anything from your module. Local variables are not exposed, you need to explicitly mark them as public.

exports.hello = hello;

hello: hello()

You have () after the variable holding the function. You are calling it and assigning the return value (which is not a function) to your hello property.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335