An error encountered while trying to utilize the npm convert-units package within an Ionic 4 application

In my Ionic 4 app, I am utilizing version 2.3.4 of the npm package called convert-units.

To install this package in my Ionic 4 application, I used the CLI command: npm i convert-units --save

However, upon importing the library with

import { convert } from "convert-units"
and trying to use a function like convert().list() in my component file, an error is being displayed.

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 have attempted the following methods:

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 included this code in index.html which resolved the issue.

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

Answer №1

Check out this code snippet:

import * as convert from 'convert-units'

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

Here's the 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

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Creating a dynamic configuration for service instantiation in Angular 4/5

Currently working on a library that has an AuthModule with an AuthService for managing OAuth2 authentication using oidc-client-js. I want the application using this library to be able to set up the configuration for the OAuth client. One way to do this is ...

Utilize the service method in Karma Jasmine from the component

I am facing an issue with the code in my component: ngOnInit() { this.formattedPacks = this.protocolPackService.formatPacks(this.selectedPacks); } During testing, I encounter the following error: this.protocolPackService.formatPacks is not a functio ...

The TypeScript compilation is not able to find index.ts at the moment

When I tried to run 'ng serve', I encountered the following error message: The TypeScript compilation is missing node_modules/angular2-indexeddb/index.ts. It is crucial to ensure that this file is included in your tsconfig under the 'file ...

'value' is connected to every single hook

Every time I try to save my work using any hook, the 'value' field changes automatically. This has been causing me a great deal of stress. I would be extremely grateful if someone could assist me with this issue. click here for image description ...

Angular 7 and Express: No content returned in response body after making a POST request

I am encountering an issue with retrieving the response from a POST request in Angular 7. When I set the API to return "text," everything works as expected. However, when I change the response to JSON, the response body in Angular appears to be null. Test ...

Error Encountered: ExpressionChangedAfterItHasBeenCheckedError in Shared Service

Why am I receiving a warning in the console even though everything seems to be functioning correctly in Angular? How can this issue be resolved? You can find the StackBlitz here I understand that one possible solution is to use parent-child communication ...

Exploring Substrings in TypeScript strings

Can you pass a partial string (substring) as a value to a function in TypeScript? Is something like this allowed? function transform( str: Substring<'Hello world'> ) { // ... } If I call the function, can I pass a substring of that st ...

The data type 'number' cannot be assigned to the data type 'undefined'. Error code: ts(2322)

I encountered an issue where it's giving me an error stating that type number cannot be assigned to type undefined on the last digit (1) in scale={[1.618, 1, 1]}. Can anyone help me figure out how to resolve this TypeScript error? "use client&quo ...

Angular - Utilizing NgRx selector for efficient data update notifications

Is there a method to create a "data updated" indicator when I am not interested in the actual updated data itself? Consider a scenario with a reducer: const initialState: SomeReducer = { dataInQuestion: Array<SomeDto>, ... } Following an action ...

After ngAfterViewInit, the @ViewChild element has not been defined

@ViewChild('videoPlayer') myVidElement: ElementRef; There is an HTML5 video element in the markup with the ViewChild specified as above However, I am facing an issue where the element is not accessible and is logged as undefined in both ngOnIni ...

What is the correct way to implement Vue.use() with TypeScript?

I am trying to incorporate the Vuetify plugin into my TypeScript project. The documentation (available at this link) suggests using Vue.use(), but in TypeScript, I encounter the following error: "error TS2345: Argument of type '{}' is not assign ...

How can variables from state be imported into a TypeScript file?

Utilizing vue.js along with vuetify, I have a boolean value stored in state via Vuex defined in src/store/index.ts (named darkMode). This value is used within one of my view components inside a .vue file. However, I now wish to access the same variable in ...

Angular file upload component with customizable file size limits

I'm currently developing an upload file feature that will transmit the file via nodejs. However, I am encountering an issue related to file size. Whenever the file exceeds a few kilobytes, I encounter the following error: Error: request entity too la ...

Error message encountered following the removal of an undesirable type from an array in Typescript

When working with TypeScript, I am facing an issue. I have an array defined as Array<string|undefined, and my goal is to filter out the undefined values from this array and assign the resulting array to a variable of type Array<string>. However, I ...

Similar to AngularJS Component's "require" property, Angular Component also has an equivalent

In the process of updating a sizable Angular 1.6 App, we encounter numerous components that utilize 'require' to access the parent component's controller. The structure of an AngularJS component appears as follows: var tileTextbox = { ...

Populating a data grid with several objects within a JSON object

I am currently developing a project utilizing React with typescript and materialUi. My task is to retrieve data from a JSON fetch request and display it in a DataGrid. The structure of the JSON data is as follows: { id: "1234567890", number: ...

extract objects from an array based on a specific property that meets a given condition

Currently, I am facing the challenge of filtering an array of objects based on a specific property that matches what the user has entered. Here is the array I am working with: peopleList = [ {name: "john bee", age:23}, {name: "john woo", age:43}, ...

"Exploring the incredible powers of Ionic2, Angular2, HTTP requests, and

Despite all the research I've done on observables, I still struggle to grasp how they function. The HTTP request code snippet is as follows: import { Component, OnInit, Injectable } from '@angular/core'; import { Http, Response, Headers, R ...

Setting a default value for a complex prop in Vue through Type-based props declarations

I'm struggling with this issue: Argument of type 'HelloWorldProps' is not assignable to parameter of type 'InferDefaults<LooseRequired<HelloWorldProps>>'. Types of property 'complexProp' are incompatible.ts( ...

Retrieving an Enum member based on its value in TypeScript

I am working with an enum called ABC: enum ABC { A = 'a', B = 'b', C = 'c', } In addition, I have a method named doSomething: doSomething(enum: ABC) { switch(enum) { case A : console.log(A); break; case ...