TypeError: Variable is not a constructor

Hey! I'm pretty new to Node-RED (and Node.JS), but have been loving using it for some home automations. My latest project is to pull hourly meter readings from the power company via an API that they provide. Someone else has done the hard work of developing the Node.JS library to access the information, but I'm trying to adapt that to Node-RED using the function node. I'm having some trouble converting it and was hoping someone could provide a little advice. The repo can be found at
github.com/apearson/southern-company-api.

I believe that the first part:

/* Importing Library */
import {SouthernCompanyAPI} from 'southern-company-api';
/* Or requiring for a script */
var SouthernCompanyAPI = require('../southern-company-api').SouthernCompanyAPI;`Preformatted text`

Is supposed to be configured in the new "Setup" window in Node-RED. I've created a module named "southern-company-api" and imported as "SouthernCompanyAPI". I've also commented out that section in the "On Message" tab where the rest of my code is. However, now I'm getting

TypeError: SouthernCompanyAPI is not a constructor

which I believe is due to the next section of code (populated with my login info in the actual code):

/* Instantiating API */
const SouthernCompany = new SouthernCompanyAPI({
  username: 'username',
  password: 'password',
  accounts: ['123123123']
});

Is there a different function or construction I should be using in order to instantiate this constant "SouthernCompany"?

Are there any other obvious issues you see that I'm going to run into later in the code? Any help is appreciated!

/* Importing Library */
import {SouthernCompanyAPI} from 'southern-company-api';
/* Or requiring for a script */
var SouthernCompanyAPI = require('../southern-company-api').SouthernCompanyAPI;

/* Instantiating API */
const SouthernCompany = new SouthernCompanyAPI({
  username: 'username',
  password: 'password',
  accounts: ['123123123']
});

/* Listening for login success */
SouthernCompany.on('connected', ()=>{
  console.info('Connected...');

  async function fetchMonthly() {
    /* Getting Monthly Data */
    const monthlyData = await SouthernCompany.getMonthlyData();

    /* Printing Monthly Data */
    console.info('Monthly Data', JSON.stringify(monthlyData));
  }
  fetchMonthly();

  async function fetchDaily() {
    /* Getting Daily Data */
    const startDate = new Date(2020, 2, 1);
    const endDate = new Date();
    const dailyData = await SouthernCompany.getDailyData(startDate, endDate);

    /* Printing daily data */
    console.info('Daily Data', JSON.stringify(dailyData));
  }
  fetchDaily();
});

/* Listening for any errors */
SouthernCompany.on('error', console.error);

For some background, I'm using the Node-RED v2.2.2 add-on in HomeAssistant on a Raspberry Pi. I'm not sure what the Node.JS version is, but everything is as up to date as I can get it.

Hello @ibejtm.
Welcome to this forum!

While I don't know any details regarding the Southern Company API, I see an issue in those lines: You have to use either of them to import or require the API. Coding for Node-RED, you require libraries, so the second syntax would be the correct one.

But: You cannot require additional libraries by code in the Function node. The documentation explains the two alternative ways to follow - either by Using the functionGlobalContext option or by Using the functionExternalModules option.

Hope this helps,
Ralph

2 Likes

Okay, thanks for the help! I read through the function node documentation before posting, but I thought I was importing the library fine using this:

I'll try the functionGlobalContext. I couldn't see functionExternalModules in my settings.js file even though I'm on Node-RED v2.2.2, but that may be a Home Assistant Addon thing. I get married in a week, and it has been cutting into my troubleshooting time!

Why? If the function node import not working for you?

This is the function node import screen right?

image

My settings.js file is apparently a version modified for home assistant and so there is no functionExternalModules to enable. Something definitely installed the module in my my node_modules folder:

image

Is there something else I need to do for this code? I don't see how to "require" something from the setup screen.

/* Importing Library */
import {SouthernCompanyAPI} from 'southern-company-api';
/* Or requiring for a script */
var SouthernCompanyAPI = require('../southern-company-api').SouthernCompanyAPI;

Apart from use the import - no.

Have you written code in the "On Message" tab to start using it?

Yes, everything else is in "On Message". When I trigger it though I get a

"TypeError: SouthernCompanyAPI is not a constructor"

from this section

/* Instantiating API */
const SouthernCompany = new SouthernCompanyAPI({
  username: 'email',
  password: 'password',
  accounts: ['account']
});
  • comment out everything in your function
  • add node.warn(SouthernCompanyAPI)

what do you see in debug window?

image

ok, so try ...

const SouthernCompany = new SouthernCompanyAPI.SouthernCompanyAPI({
  username: 'email',
  password: 'password',
  accounts: ['account']
})
node.warn(SouthernCompany);
1 Like

image

I'm not sure if the second message is something important

Well lets put it this way, you have no error now.

Start coding.

1 Like

Actually that was node warn on the SouthernCompanyAPI, not SouthernCompany. Here you go:

image

yep - looks good.

Haha, okay, thanks!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.