I'm having trouble trying to get my class working in my node.js file. When I require the module I wrote, the require './module' calls my constructor and gives an error. But I actually want to instantiate later on in the file.
i.e
class Mic
constructor: (x) ->
@t = []
@t.push x
exports.Mic = Mic
and here is my app.coffee file
require 'coffee-script'
require './Mic'
When I run app.coffee it gives an exception ReferenceError: x is not defined. Which makes sense since its calling the constructor, but why is it calling the constructor even though I havent called new Mic ?
Edit After fixing the indentation
class Mic
constructor: (x) ->
@t = []
@t.push x
exports.Mic = Mic
and updating my app.coffee to
Mic = require './Mic'
m = new Mic 3
console.log m
I get the error
TypeError: object is not a function
at Object.CALL_NON_FUNCTION_AS_CONSTRUCTOR (native)
First thing's first: you don't need the
require 'coffee-script'
—running it withcoffee
is enough; same as running the compiled JavaScript. You don't need the CoffeeScript library available at runtime in your program.Secondly, the first file appears indented incorrectly; if you want that to be
Mic
's constructor, indent it one level underneath theclass
, i.e.:Finally, the issue is that
exports
is an object of exports. See here:You've assigned
Mic
to theexports
object'sMic
key, so nowexports
in Mic.coffee looks like this:When you say
require './Mic'
, you're getting that object back; in other words:So you need to do one of the following:
Export
Mic
as the entire export of Mic.coffee, and not as a key:Get the entire module out, and then instantiate the
Mic
object within:Just take
Mic
out from therequire
'd module: