TypeError: [c++ addon class] is not a constructor

308 views Asked by At

TypeError: m.Default is not a constructor is the error I'm getting when I try to make a JavaScript class by using NAN.

Here's what I'm doing:

//AudioDeviceController.h
#pragma once
#include <nan.h>

class Default : public Nan::ObjectWrap 
{
public:
   static Nan::Persistent<v8::FunctionTemplate> constructor;
   static NAN_MODULE_INIT(Init);
   static NAN_METHOD(New);
   static NAN_GETTER(RoleGetter);
   static NAN_SETTER(ReadOnly);
};

AudioDeviceController.cc:

#include "AudioDeviceController.h"
Nan::Persistent<v8::FunctionTemplate> Default::constructor;

NAN_MODULE_INIT(Default::Init)
{
   v8::Local<v8::FunctionTemplate> construct = Nan::New<v8::FunctionTemplate>(Default::New);
   constructor.Reset(construct);
   construct->InstanceTemplate()->SetInternalFieldCount(1);
   construct->SetClassName(Nan::New("Default").ToLocalChecked());

   Nan::SetAccessor(construct->InstanceTemplate(), Nan::New("media").ToLocalChecked(), Default::RoleGetter, Default::ReadOnly);
   Nan::SetAccessor(construct->InstanceTemplate(), Nan::New("communications").ToLocalChecked(), Default::RoleGetter, Default::ReadOnly);

   target->Set(Nan::New("Default").ToLocalChecked(), construct->GetFunction());
}

NAN_METHOD(Default::New)
{
   if(!info.IsConstructCall())
   {
      return Nan::ThrowError(Nan::New("In C++: Default::New - called without new keyword").ToLocalChecked());
   }

   if(info.Length() != 0)
   {
      return Nan::ThrowError(Nan::New("In C++: Default::New - Expected 0 parameters").ToLocalChecked());
   }

   Default *def = new Default();
   def->Wrap(info.Holder());

   info.GetReturnValue().Set(info.Holder());
}

NAN_GETTER(Default::RoleGetter)
{
   //return the correct role
}

NAN_SETTER(Default::ReadOnly)
{
   return Nan::ThrowError(Nan::New("Property is read only").ToLocalChecked());
}

index.cc:

#include <nan.h>
#include "AudioDeviceController.h"

NAN_MODULE_INIT(InitModule)
{
   Default::Init(target);
}

NODE_MODULE(winCoreAudio, InitModule);

bindings.gyp:

{
    "targets": [{
        "target_name": "winCoreAudio",
        "include_dirs" : [
            "src",
            "<!(node -e \"require('nan')\")"
        ],
        "sources": [
            "src/index.cc",
            "src/asyncCBclass.cpp",
            "src/AudioDeviceController.cc",
         "src/AudioDevice.cc",
         "src/WinAPIWrap.cpp",
        ]
    }]
}

index.js:

var m = require('./');

var deflt = new m.Default();

and here is the full error output:

var deflt = new m.Default();
            ^
TypeError: m.Default is not a constructor
    at Object.<anonymous> (C:\Users\Prime\source\repos\uvAsyncStuff\index.js:25:13)
    at Module._compile (module.js:652:30)
    at Object.Module._extensions..js (module.js:663:10)
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12)
    at Function.Module._load (module.js:497:3)
    at Function.Module.runMain (module.js:693:10)
    at startup (bootstrap_node.js:191:16)
    at bootstrap_node.js:612:3

The outcome I'm trying to achieve is the ability to use the new keyword in JavaScript to be able to create various objects. I also need to be able to, using C++, instantiate new JS objects that I've declared in C++ code. I can only guess that Node is not recognizing the objects I'm declaring. I have several object like this in the project I am working on, this one was the simplest to give as an example.

The full project that this will be used with can be found here.

  • I am compiling with Windows 10
  • Node version: 8.11.3
  • NAN version: 2.10.0
  • node-gyp version: 3.7.0.
1

There are 1 answers

0
Jack On BEST ANSWER

For me, I was able to fix this error by directly requiring ./build/Release/[insert-module-name].node at the beginning of index.js