2

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

3
  • 2
    And how do calcs.js's export statements look like? Commented Jul 18, 2020 at 16:25
  • @mbojko It is just a file full of functions like above - I don't have any export statements in it Commented Jul 18, 2020 at 16:40
  • You need to export calculateCosts function. just add export before that. like export function calculateCosts(){..}. typescriptlang.org/docs/handbook/modules.html Commented Jul 18, 2020 at 21:20

1 Answer 1

1

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;
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.