3

I need to use a JavaScript library in my CoffeeScript application. Since I am not familiar with both languages I try something simple. My coffeescript file:

empty = require('models/empty')

    class Contact extends Spine.Model
      @configure 'Contact', 'name', 'email'

      @extend Spine.Model.Local

      create: -> 
        empty.one()
        super

    module.exports = Contact

And my Javascript file named empty.js :

console.log('what')

function one () {
    console.log('one')
};

The coffeescript file works normally, although I cant get empty.one() to work. 'what' is printed on console which means that the JS file is loaded. Although I get the following error when one() is called:

Uncaught TypeError: Object # has no method 'one'

I have tried many different ways of defining the function, as variable, and using different syntaxes I found on tutorial, although none of this seems to work. Can someone point the mistake I am making?

1 Answer 1

6

You need to export the function like this:

function one () {
    console.log('one')
};
exports.one = one;

Then it will be accessible from other modules that require it.

(I assume that you use node.js or any other commonjs-like platform)

Sign up to request clarification or add additional context in comments.

1 Comment

Why did I not notice this anywhere?! Thank you I was going crazy! Its working now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.