Issue with using npm convert-units package in ionic 4

Viewed 417

I am using the convert-units 2.3.4 npm package in ionic 4.

I installed this package in my ionic 4 app using cli - npm i convert-units --save

But when I am importing the library using import { convert } from "convert-units" and using one of the functions like convert().list() in my component file then it is showing an error.

import { convert } from "convert-units";

calculatePrice(){
  console.log(convert().list());
}
Uncaught ReferenceError: global is not defined
    at Object../node_modules/lodash.support/index.js (index.js:30)
    at __webpack_require__ (bootstrap:83)
    at Object../node_modules/lodash._basecreatecallback/index.js (index.js:12)
    at __webpack_require__ (bootstrap:83)
    at Object../node_modules/lodash.foreach/index.js (index.js:9)
    at __webpack_require__ (bootstrap:83)
    at Object../node_modules/convert-units/lib/index.js (index.js:3)
    at __webpack_require__ (bootstrap:83)
    at Module../src/app/components/ingredients-value/ingredients-value.component.ts (ingredients-value.component.ts:9)
    at __webpack_require__ (bootstrap:83)

I already tried this method:

const convert = require('convert-units');

or

var convert = require('../../../../node_modules/convert-units/lib')
  , assert = require('assert')
  , tests = {}
  , ACCURACY = 1/1000
  , percentError = require('../../../../node_modules/convert-units/lib/percentError');

Solution:- I add this code in index.html. This solved my issue.

 <script>
    var global = global || window;
  </script>
1 Answers

try this snippet:

import * as convert from 'convert-units'

 console.log( convert().measures());
 console.log(convert(1).from('l').to('ml'));

output:

(23) ["length", "area", "mass", "volume", "each", "temperature", "time", "digital", "partsPer", "speed", "pace", "pressure", "current", "voltage", "power", "reactivePower", "apparentPower", "energy", "reactiveEnergy", "volumeFlowRate", "illuminance", "frequency", "angle"]0: "length"1: "area"2: "mass"3: "volume"4: "each"5: "temperature"6: "time"7: "digital"8: "partsPer"9: "speed"10: "pace"11: "pressure"12: "current"13: "voltage"14: "power"15: "reactivePower"16: "apparentPower"17: "energy"18: "reactiveEnergy"19: "volumeFlowRate"20: "illuminance"21: "frequency"22: "angle"length: 23__proto__: Array(0)
intro.page.ts:60 1000
Related