1

I use numeral.js in both the viewmodels and sometimes in the html of my knockout components.

<div data-bind="text: numeral(totalCurrent()).format('$0,0.00')"></div>

I am using webpack to package my html and js together which seem to work great, however now I get the error 'numeral is not defined' if I try to use numeral in the html markup as shown above.

Numeral works as expected in the viewmodel js just fine.

Any ideas on how I might fix this??

Jason Coley
  • 125
  • 9

1 Answers1

0

You're getting the error as the function numeral is not available in the global scope i.e window as webpack will wrap everything in a IIFE.

You try putting numeral onto the global scope in with something like the following.

window.numeral = require( 'numeral');
Bulkan
  • 2,555
  • 1
  • 20
  • 31
  • OK, so I added `window.numeral = require('numeral');` in my entry point constructor and it appears to have worked, cheers. should this also work if the windows.numeral is added to the plugins section of webpack... something like.. `new webpack.ProvidePlugin({ numeral: 'numeral', 'windows.numeral': 'numeral' }),` – Jason Coley Jul 15 '17 at 03:26
  • @JasonColey No problem. Using `ProvidePlugin` should also work. – Bulkan Jul 16 '17 at 06:26