Encountered an unexpected * token while importing lodash

Encountering an issue with TypeScript compilation after installing lodash library.

To install lodash, run the following commands: 1) npm install --save lodash 2) npm install --save lodash

Code snippet:

import * as lodash from 'lodash';
class MyDummyClass {
    constructor() {
        console.log('Hello Im a constructor');
    }
    foo() {
        console.log(lodash.chunk(['a', 'b', 'c', 'd'], 2));
    }
}
const dummyClass = new MyDummyClass();
dummyClass.foo();

Executing with:

npx ts-node my-dummy-class.ts

Encountering the error:

Unexpected token *

Any suggestions on how to resolve this issue?

Answer №1

Attempt to include the lodash Node Package Manager (NPM) by utilizing the require method:

import lodash = require('lodash');

Answer №2

Give this a go

const _ = require('lodash');
class MyTestClass {
    constructor() {
        console.log('Greetings! I am the constructor');
    }
    bar() {
        console.log(_.chunk(['x', 'y', 'z', 'w'], 2));
    }
}
const testClass = new MyTestClass();
testClass.bar();

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

"Looking to incorporate dynamic Carousel Indicators into your Angular2 project? Here's how you can

After moving to just one slide, the "next" and "prev" buttons stop working. Additionally, clicking on the indicators to move slides is also ineffective. Interestingly, when I remove the div with the class carousel-indicators, the "next" and "prev" buttons ...

Having issues with referencing external JavaScript and CSS files in Angular 6

Dealing with an angular6 project and a bootstrap admin dashboard template, I'm facing issues importing the external js references into my Angular application. Looking for guidance on how to properly import and refer to external Js/CSS files in an angu ...

Is there a way to retrieve the chosen value from an ion-alert radio alert?

async showAlertRadio(heading:string){ const alert = await this.alertCtrl.create({ header: heading, inputs :[ { name : 'Radio 1', type: 'radio', label: 'Radio 1', ...

Exploring the discrepancies in utilizing the i18n library versus directly incorporating locale from JSON in vue.js

Recently, I decided to incorporate Chinese language into my Vue app. To achieve this, I loaded the entire JSON text into Vuex and utilized the stored text directly, instead of relying on an i18n library. I'm curious to know if there are any potential ...

Invoking a method in a derived class upon completion of asynchronous logic within the base class

Currently, I am in the process of building an Angular application. One aspect of my project involves a class that extends a base class. While this approach may not be ideal, I am curious to know what would be the best practice for BaseClass to trigger me ...

Issues with redirecting to HTTPS on the homepage in Node.js and Express causing problems

I have set up two servers - one for http and the other for https on my VPS using Express: var httpServer = http.createServer(app); httpServer.listen(httpPort); var httpsServer = https.createServer(credentials, app); httpsServer.listen(httpsPort); To red ...

Using AngularJS $resource to send query strings instead of JSON objects in a POST request (Typescript)

Whenever I create a custom $resource action like this: getEntityResource(): ng.resource.IResourceClass<IEntityResource> { let addAction: ng.resource.IActionDescriptor = { method: 'POST', url: 'http://l ...

What is the best way to mock imports in NestJS testing?

I am interested in writing a unit test for my nestjs 'Course' repository service, which has dependencies on Mongoose Model and Redis. courses.repository.ts: import { Injectable, HttpException, NotFoundException } from "@nestjs/common"; ...

A recursive approach for constructing a tree structure in Angular

Currently, I am working on a project involving the implementation of crud functions. To display the data in a tree-like structure, I am utilizing the org chart component from the PrimeNg library. The data obtained from the backend is in the form of an arra ...

Personalizing the arrow positioning of the Angular8 date picker (both top and bottom arrow)

I am interested in enhancing the design of the Angular 8 date picker by adding top and bottom arrows instead of the default left and right arrows. Can someone guide me on how to customize it? Check out the Angular 8 date picker here ...

Issues with Rxjs pipe and Angular's Http.get() functionality are causing complications

Working with an Angular 4 Component that interacts with a Service to fetch data is a common scenario. Once the data is retrieved, it often needs to be transformed and filtered before being utilized. The prevailing method for this task is through the use of ...

What could be causing the issue of console.log() being called multiple times in Angular when invoking a method through text interpolation?

Utilizing Text interpolation to invoke a method. home.component.html <p>{{myMethod()}}</p> home.component.ts import { Component, OnInit } from '@angular/core'; @Component({ selector: 'app-home', templateUrl: './h ...

A guide on how to add the chosen item within the same tag using Angular 2

I am trying to choose an item and then add the same item within the same tag, similar to how tags are selected when asking a question on Stack Overflow. This is my template: <div class="form-group" *ngFor="let i of show"> <label for="examp ...

Failure to Apply Angular Conditional Class

What is the reason for test-abc not being included? <div class="abc" [class.test-abc]="true"></div> I successfully implemented it with this syntax: [ngClass]="{'foo': true, 'abc': true}" ...

Developing a versatile table component for integration

My frontend app heavily utilizes tables in its components, so I decided to create a generic component for tables. Initially, I defined a model for each cell within the table: export class MemberTable { public content: string; public type: string; // ...

What is the rationale behind Angular's decision to use cdr.markForCheck() instead of cdr.detectChanges() in the async

Regarding Angular, I have a question: Why does the Angular async pipe use cdr.markForCheck() instead of cdr.detectChanges()? I have noticed two main differences between these two approaches: markForCheck() marks the path all the way up to the root compo ...

What methods are available to keep a component in a fixed position on the window during certain scrolling intervals?

I need help creating a sidebar similar to the one on this Airbnb page. Does anyone have suggestions for how to make a component stay fixed until you scroll past a certain section of the page? I am working with functional React and Material-UI components, ...

Implementing TypeScript in an Asp.net Core ReactJS application`s configuration

After using Visual Studio 2022 to create an asp.net core Reactjs project, I discovered that everything was written in javascript instead of typescript. Is there a way to switch this project over to typescript? ...

Discovering the power of Angular: Leveraging nativeElement to enhance your click handlers

Perhaps just a "stylish" yet effective solution... Here's what I have that is functional: // Template <div *ngFor="let media of period.media"> . . . <button #btn (click)="onDeleteClick(btn)" [attr.media-id]="media.ID"> ...

Use ngClass to dynamically change the color of a button when it is clicked. This functionality can be applied to buttons

In my application, there are 80 buttons displayed on the screen using the following code: <tr *ngFor="let plans of PlanList"> <td class="text-primary">{{plans.typename}} </td> <td class="text-center&quo ...