Using libraries from the vendors.ts file in webpack with single import

My current configuration involves the following code:

'use strict';
const path = require('path');
const webpack = require("webpack");
const ExtractTextPlugin = require("extract-text-webpack-plugin");
const autoprefixer = require('autoprefixer');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const AssetsPlugin = require('assets-webpack-plugin');
const assetsPluginInstance = new AssetsPlugin();
const CleanWebpackPlugin = require('clean-webpack-plugin');

// More code here...

Everything is working perfectly. I have vendors.js with all libraries and main.js with just the app code and logic. I would like to maintain this setup.

Let's take a look at vendors.ts:

// Polyfills
import 'angular2/bundles/angular2-polyfills.js';

// Other imports...

In this file, we are using the lodash library which functions correctly when tested in the Chrome console.

If we try to use lodash in my app.component.ts file:

export class AppComponent implements OnInit {
  constructor() {
    console.log(_.last([1, 2, 3]));
  }

  ngOnInit() {
  }
}

We encounter the error:

ERROR in [default] /home/g-montag/WebstormProjects/Chat/src/app/app.component.ts:17:16 
    Cannot find name '_'.

To resolve this issue, we need to import the library again, but doing so will result in lodash being imported into our main.js file as well, leading to duplicate code.

So, the ultimate question arises: How can we import lodash or another library in vendors.ts and utilize it in the app without repetitive imports?

Answer №1

Encountered an issue with this error message: ERROR in [default] /home/g-montag/WebstormProjects/Chat/src/app/app.component.ts:17:16 Cannot find name '_'

This error occurs during compile time. A quick fix is to create a file named vendor.d.ts with the following content:

declare var _:any;

Learn More

For more information on ambient declarations, visit: https://basarat.gitbooks.io/typescript/content/docs/types/ambient/d.ts.html

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

Angular async validator doesn't update form field error status

I have implemented an async validator for the zip field: zip: ['', { validators: [ Validators.required, Validators.minLength(5), Validators.maxLength(5) ], asyncValida ...

Analyzing conditional statements in React with State management

I've been encountering challenges with if statements correctly evaluating, displaying either true all the time or exhibiting unexpected behavior when passing variables or using state variables for evaluation. I realize this might be related to differe ...

Retrieving data from mongodb individually

I have a database table called Questions. My goal is to display the first question and then upon user interaction, show them the next one. To retrieve the first question, I use the following query: Template.home.user_questions = function () { return Qu ...

Utilizing the Twitter API variable within ExpressJS while incorporating AngularJS

Using the "twit" Twitter package from GitHub, I am able to call the Twitter API and retrieve data that is logged in the console. However, I am unsure of how to pass this data to AngularJS in order to display the tweets on the front-end. T.get('search ...

Error in Angular 2 component when loading background images using relative URLs from an external CSS skin

In my angular2 component, I am utilizing a third-party JavaScript library. The skin CSS of the component attempts to load images using relative URL paths. Since I am following a component-based architecture, I prefer to have all component dependencies enca ...

Step-by-step guide on sorting WordPress posts by a custom field date

I've created an event sidebar section that will only show the next 3 upcoming events. I have successfully set up the custom post type and custom fields, but I am struggling to figure out how to order the posts based on the start date of the events, wh ...

Template reference does not connect to the ApexChart graphic

As a newcomer to ApexCharts.js, I am currently working on setting up a Vue3 app using Composition API along with the setup syntax. My goal is to reference the template ref of 'apexchart' so that I can later call the dataURI() function on it. I am ...

What is the most effective method for preserving RichText (WYSIWYG output)?

I am currently using a JavaScript-based rich text editor in my application. Could you suggest the most secure method to store the generated tags? My database is MySQL, and I have concerns about the safety of using mysql_real_escape_string($text);. ...

What is the best way to correctly link each object from a for loop to its corresponding DOM element?

I'm looking for a solution that resembles the following code snippet: <select> <option [bound]="items[i]" *ngFor="let item of items; #i = index">{{item}}</option> </select> ...

The jQuery AJAX call isn't getting the expected response

Having trouble with a specific part of an application I'm working on. I'm trying to make an AJAX request to fetch information from an XML file. The server is supposed to return the information in JSON format. I've used the same code for bot ...

Hover over the text only, not the entire row

Is there a way to create a mouse hover effect on text only, without affecting the entire row? I attempted using Position(), but didn't have success. Here is a link to the fiddle I created: <ul id='ulid'> <li>Task1</li> < ...

Reorganize mat-table rows using Angular Material's drag-and-drop feature

One of the new features that came with Angular 7 is the powerful DragDropModule: https://material.angular.io/cdk/drag-drop/examples The documentation covers rearranging items in lists and transferring them between different lists, but it doesn't ment ...

Managing state on the login page functions properly, although there is a minor inconvenience of having to click the login button twice after entering the username and password

In Login.tsx, I store user/pass info in a useState called login and pass it up to App.tsx. I then store the access property from login useState to access useState inside App.tsx. While this does technically work, there is an issue where it seems to be one ...

Understanding the concept of Angular factories in Javascript

app.service('WeatherService', function($http) { var service = {}; service.getLocation = function() { return $http.jsonp("http://ipinfo.io/json?callback=JSON_CALLBACK"); }; service.getCurrentWeather = function(city) { var ...

Frustration mounts as Angular 2 Jasmine Test encounters setbacks

Just diving into the world of Angular 2 and core testing. Currently, I have a MovieComponent that relies on an injected MovieService. To better understand the process, I decided to use route parameters. So far, my code is running smoothly without any issu ...

Use ng2-dragula to organize elements within a div container

During my exploration of implementing drag and drop functionality, I stumbled upon the ng2-dragula project. I found myself wondering how to group the following structure into a single 'drake' entity: <div> <img .../> <img .. ...

Utilizing ng-options within an ng-repeat to filter out previously chosen options

Facing a simple problem and seeking a solution. I am using ng-repeat to dynamically create select boxes with ng-options displaying the same values. The ng-repeat is used to iterate through model options. <div data-ng-repeat="condition in model.condit ...

Dynamically update rows in a React.js component by manipulating hooks for adding or

I am currently exploring the use of hooks in react and experimenting with creating a basic website where users can input 2 values and have them added to a list. Each entry in the list also has a delete button associated with it. Oddly enough, I have encou ...

Encountered an issue with mapping data from a controller to a view in Angular.js

Currently, my application consists of only three small parts: a service that makes an http call to a .json file, a controller that receives data from the service and sends it to a view. Everything was working fine when I hard coded the data in my service. ...

Whenever I include an onClick event to a div element, the entire webpage fails to display

Currently taking on the task of developing a seat booking website. I am encountering an issue with adding an event listener to a particular div element, which should trigger a function to store the value of the div. However, upon implementing the onClick e ...