# TypeError: Variable is not a constructor

**URL:** https://discourse.nodered.org/t/typeerror-variable-is-not-a-constructor/59682
**Category:** General
**Tags:** function-node
**Created:** [10 March 2022 21:52 UTC](https://discourse.nodered.org/t/typeerror-variable-is-not-a-constructor/59682 "2022-03-10T21:52:16Z")
**Posts on this page:** 16
**Page:** 1

<div class="post-metadata">

### Author: ![ibejtm](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ibejtm/32/58229_2.png) [@ibejtm](https://discourse.nodered.org/u/ibejtm)
#### Post date: [10 March 2022 21:52 UTC](https://discourse.nodered.org/t/typeerror-variable-is-not-a-constructor/59682/1 "2022-03-10T21:52:16Z")

</div>

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](https://github.com/apearson/southern-company-api).

I believe that the first part:

```auto
/* 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

```auto
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):

```auto
/* 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!

```auto
/* 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.

---

<div class="post-metadata">

### Author: ![ralphwetzel](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ralphwetzel/32/53713_2.png) [@ralphwetzel](https://discourse.nodered.org/u/ralphwetzel)
#### Post date: [13 March 2022 07:43 UTC](https://discourse.nodered.org/t/typeerror-variable-is-not-a-constructor/59682/2 "2022-03-13T07:43:33Z")

</div>

Hello @ibejtm.  
Welcome to this forum!

> [@ibejtm](#):
>
> ```auto
> /* Importing Library */
> import {SouthernCompanyAPI} from 'southern-company-api';
> /* Or requiring for a script */
> var SouthernCompanyAPI = require('../southern-company-api').SouthernCompanyAPI;
> 
> ```

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](https://nodered.org/docs/user-guide/writing-functions#using-the-functionglobalcontext-option) or by [Using the functionExternalModules option](https://nodered.org/docs/user-guide/writing-functions#using-the-functionexternalmodules-option).

Hope this helps,  
Ralph

---

<div class="post-metadata">

### Author: ![ibejtm](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ibejtm/32/58229_2.png) [@ibejtm](https://discourse.nodered.org/u/ibejtm)
#### Post date: [15 March 2022 20:36 UTC](https://discourse.nodered.org/t/typeerror-variable-is-not-a-constructor/59682/3 "2022-03-15T20:36:19Z")

</div>

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:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/4/943f2ee7c5378d55195ed23df3b2f5096356f5db.png)

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!

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [15 March 2022 22:22 UTC](https://discourse.nodered.org/t/typeerror-variable-is-not-a-constructor/59682/4 "2022-03-15T22:22:52Z")

</div>

> [@ibejtm](#):
>
> I'll try the functionGlobalContext

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

---

<div class="post-metadata">

### Author: ![ibejtm](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ibejtm/32/58229_2.png) [@ibejtm](https://discourse.nodered.org/u/ibejtm)
#### Post date: [17 March 2022 14:37 UTC](https://discourse.nodered.org/t/typeerror-variable-is-not-a-constructor/59682/5 "2022-03-17T14:37:00Z")

</div>

> [@Steve-Mcl](#):
>
> Why? If the function node import not working for you?

This is the function node import screen right?

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/3/6386acfb6816ef7df43b7c5c7188a7c5e628dc83.png)

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](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/2/621f47e23c0fbe359b1e59b15564d07e879d2a94.png)

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

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

```

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [17 March 2022 14:38 UTC](https://discourse.nodered.org/t/typeerror-variable-is-not-a-constructor/59682/6 "2022-03-17T14:38:21Z")

</div>

> [@ibejtm](#):
>
> Is there something else I need to do for this code?

Apart from use the import - no.

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

---

<div class="post-metadata">

### Author: ![ibejtm](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ibejtm/32/58229_2.png) [@ibejtm](https://discourse.nodered.org/u/ibejtm)
#### Post date: [17 March 2022 14:39 UTC](https://discourse.nodered.org/t/typeerror-variable-is-not-a-constructor/59682/7 "2022-03-17T14:39:45Z")

</div>

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

"TypeError: SouthernCompanyAPI is not a constructor"

from this section

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

```

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [17 March 2022 14:41 UTC](https://discourse.nodered.org/t/typeerror-variable-is-not-a-constructor/59682/8 "2022-03-17T14:41:24Z")

</div>

- comment out everything in your function
- add `node.warn(SouthernCompanyAPI)`

what do you see in debug window?

---

<div class="post-metadata">

### Author: ![ibejtm](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ibejtm/32/58229_2.png) [@ibejtm](https://discourse.nodered.org/u/ibejtm)
#### Post date: [17 March 2022 14:43 UTC](https://discourse.nodered.org/t/typeerror-variable-is-not-a-constructor/59682/9 "2022-03-17T14:43:03Z")

</div>

> [@Steve-Mcl](#):
>
> node.warn(SouthernCompanyAPI)

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/4/64e1db4d7175e52f589c7ed1fec0a6690791595b.png)

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [17 March 2022 14:44 UTC](https://discourse.nodered.org/t/typeerror-variable-is-not-a-constructor/59682/10 "2022-03-17T14:44:13Z")

</div>

ok, so try ...

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

```

---

<div class="post-metadata">

### Author: ![ibejtm](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ibejtm/32/58229_2.png) [@ibejtm](https://discourse.nodered.org/u/ibejtm)
#### Post date: [17 March 2022 14:46 UTC](https://discourse.nodered.org/t/typeerror-variable-is-not-a-constructor/59682/11 "2022-03-17T14:46:36Z")

</div>

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/7/a700c6d317ddf46b347583e740dce66719bfc5ef.png)

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

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [17 March 2022 14:47 UTC](https://discourse.nodered.org/t/typeerror-variable-is-not-a-constructor/59682/12 "2022-03-17T14:47:19Z")

</div>

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

Start coding.

---

<div class="post-metadata">

### Author: ![ibejtm](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ibejtm/32/58229_2.png) [@ibejtm](https://discourse.nodered.org/u/ibejtm)
#### Post date: [17 March 2022 14:47 UTC](https://discourse.nodered.org/t/typeerror-variable-is-not-a-constructor/59682/13 "2022-03-17T14:47:32Z")

</div>

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

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/1/f17776f85c635d0932d7f4f62bb8cf1564c6e53a.png)

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [17 March 2022 14:47 UTC](https://discourse.nodered.org/t/typeerror-variable-is-not-a-constructor/59682/14 "2022-03-17T14:47:52Z")

</div>

yep - looks good.

---

<div class="post-metadata">

### Author: ![ibejtm](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ibejtm/32/58229_2.png) [@ibejtm](https://discourse.nodered.org/u/ibejtm)
#### Post date: [17 March 2022 14:48 UTC](https://discourse.nodered.org/t/typeerror-variable-is-not-a-constructor/59682/15 "2022-03-17T14:48:17Z")

</div>

Haha, okay, thanks!

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)
#### Post date: [31 March 2022 14:48 UTC](https://discourse.nodered.org/t/typeerror-variable-is-not-a-constructor/59682/16 "2022-03-31T14:48:51Z")

</div>

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