Tom O'Brien
Tom O'Brien

Reputation: 1831

Angular Component using Javascript Library function throwing Type Error: is Not a Function

I have written a simple Javascript library to clump together some functions that number of Angular Components need to use. The calcs.js library has f few functions like this in it:

function calculateCosts(object) {

  do some stuff.....
  return answer;
}

I include the library in my Angular component like this:

import * as calc from '../scripts/calcs.js';

I then call this function from within my ngInit() like so:

ngInit() {
  var costs = calc.calculateCosts(stuff);
}

It compiles and upon runtime it throws the following error:

ERROR TypeError: _scripts_calcs__WEBPACK_IMPORTED_MODULE_5__.calculateTotalCosts is not a function

Am I doing this correctly? This is the first time I've tried writing my own javascript function library and including it in component, so I may have done something very silly.

I definitely have the correct path in the import, and the correct function name in the component

All help appreciated

Upvotes: 2

Views: 242

Answers (1)

mbojko
mbojko

Reputation: 14679

Can't import what the module isn't exporting. You can simply add export statements in your library:

export function calculateCosts(object) {
// do some stuff.....
    return answer;
}

Upvotes: 1

Related Questions