I'm working on a simple example; I can get it to work with Javascript, but there is something wrong with my CoffeeScript version.
Here is person.coffee:
module.exports = Person
class Person
constructor: (@name) ->
talk: ->
console.log "My name is #{@name}"
And here is index.coffee:
Person = require "./person"
emma = new Person "Emma"
emma.talk()
I am expecting to run index.coffee and see the console output "My name is Emma". Instead, I am getting an error saying TypeError: undefined in not a function.
To export multiple functions in JavaScript, use the export statement and export the functions as an object. Alternatively, you can use the export statement in front of the function definitions. This exports the function in question automatically and you do not need to use the export statement separately.
Put the module.exports
line at the bottom.
----person.coffee----
class Person
constructor: (@name) ->
talk: ->
console.log "My name is #{@name}"
module.exports = Person
Person = require "./person" // [Function: Person]
p = new Person "Emma" // { name: 'Emma' }
When you assign to module.exports
at the top, the Person
variable is still undefined
.
You could also write in person.coffee
:
class @Person
Then use the following in index.coffee
:
{Person} = require './person'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With