angular-cli icon indicating copy to clipboard operation
angular-cli copied to clipboard

import * as _ from "lodash"; seems to fail at runtime after upgrade to 17.3

Open shannonvc opened this issue 2 years ago • 4 comments

Command

serve

Is this a regression?

  • [X] Yes, this behavior used to work in the previous version

The previous version in which this bug was not present was

17.2.3

Description

In the process of upgrading an application to Angular 17.3 and I ran into an issue with our lodash imports no longer working properly when using the form; import * as _ from "lodash"; Our current setup does not currently use the allowSyntheticDefaultImports or esModuleInterop compilerOptions.

The error doesn't appear in the IDE. At runtime, when using lodash, all of the methods appear undefined;

  ngOnInit() {
    // 👇 Is crashing in 17.3; ERROR TypeError: _.isEqual is not a function
    if (_.isEqual(1, 1)) {
      console.log('lodash * as _ is working');
      this.lodashWorking.set(true);
    }
  }

From what I can tell, they appear to be located under _.default. instead. So _.default.isEqual(1,1) would work at runtime but not in the IDE.

Minimal Reproduction

Here's a repo where the issue occurs on 17.3; https://stackblitz.com/edit/stackblitz-starters-y33pan?file=src%2Fmain.ts

The same code with Angular 17.2.3 is working; https://stackblitz.com/edit/stackblitz-starters-nlgc6a?file=src%2Fmain.ts

Exception or Error

main.ts:5 ERROR TypeError: _.isEqual is not a function
    at _AppComponent.ngOnInit (app.component.ts:21:13)
    at callHookInternal (core.mjs:5136:14)
    at callHook (core.mjs:5163:13)
    at callHooks (core.mjs:5118:17)
    at executeInitAndCheckHooks (core.mjs:5068:9)
    at refreshView (core.mjs:12351:21)
    at detectChangesInView$1 (core.mjs:12560:9)
    at detectChangesInViewWhileDirty (core.mjs:12277:5)
    at detectChangesInternal (core.mjs:12259:9)
    at detectChangesInView (core.mjs:31605:5)

Your Environment

Angular CLI: 17.3.0
Node: 18.18.0
Package Manager: npm 10.2.3
OS: linux x64

Angular: 17.3.0
... animations, cli, common, compiler, compiler-cli, core, forms
... platform-browser, router

Package                         Version
---------------------------------------------------------
@angular-devkit/architect       0.1703.0
@angular-devkit/build-angular   17.3.0
@angular-devkit/core            17.3.0
@angular-devkit/schematics      17.3.0
@schematics/angular             17.3.0
rxjs                            7.8.1
typescript                      5.3.3
zone.js                         0.14.4

Anything else relevant?

If we enable either allowSyntheticDefaultImports or esModuleInterop and use the import form import _ from "lodash";. It works as expected but it was surprising that a minor upgrade of Angular would change this behavior. I wonder if it's an intentional change and if Angular expects us to use those flags / import style only.

Also unsure if it's a cli/builder issue or something from the angular compiler. The closest recent issue I could see was a fix(complier-cli) which does touch on imports and defaults but I am unsure if it's relevant #53695

Let me know if I need to recreate the issue in the main angular repository instead.

shannonvc avatar Mar 14 '24 18:03 shannonvc

Without explaining or understanding the issue, our workaround is to install lodash-es and them import the functions directly

import { cloneDeep, isEqual } from "lodash";

and call the functions directly

var cloneItem = cloneDeep(item);
isEqual(item, cloneItem);

It is worth noting that we had same issue with moment where we installed the moment-es library and imported with

import moment from 'moment-es6';

We didn't see anything the 17.3.0 release notes on why this might occur.

markklemm avatar Mar 15 '24 06:03 markklemm

This is pretty much a duplicate of https://github.com/angular/angular-cli/issues/26888 and the error is expected.

In version 17.2, this operation was functional with ng serve but failed with ng build. Vite has rectified this bug, causing it to fail in both scenarios now.

alan-agius4 avatar Mar 15 '24 08:03 alan-agius4

Hey, how do I enforce this to be correctly with tsconfig? Where can I find documentation/explanation on this?

ShacharHarshuv avatar Mar 27 '24 20:03 ShacharHarshuv

This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.