0

I'm trying to optimise my Angular 4 application by importing lodash functions globally to the app.module.ts. I tried:

import { some } from 'lodash/fp/some';
import _ from 'lodash/wrapperLodash';

However, I can't get the reference to that function in a component. This will throw an error:

_.some(myVar, { lat: null });

My dependencies (the ones related to the topic):

"devDependencies": {
"@angular/compiler": "^4.2.4",
"@angular/core": "^4.2.4",
"@types/lodash": "^4.14.104",
"lodash": "^4.17.5",
"typescript": "~2.3.3"
}

Error:

ERROR TypeError: __WEBPACK_IMPORTED_MODULE_0_lodash_fp_wrapperLodash___default.a.some is not a function
8
  • import * as _ from 'lodash'; in this context, 'lodash' will be the npm package Commented Mar 6, 2018 at 11:23
  • That way you are importing the whole package. what I'm trying to achieve is to reduce the size of my vendor bundle. Commented Mar 6, 2018 at 11:26
  • Ahh so you want to be specific and just bring some in, ok fair enough. Commented Mar 6, 2018 at 11:28
  • Ok. So I'm getting kind of error <pre>lodash_wrapperLodash___default.a.find is not a function<code> Commented Mar 6, 2018 at 11:31
  • If you only want 'some', you should do: import { some } from 'lodash'; Commented Mar 6, 2018 at 11:56

1 Answer 1

1

I still don't understand why people use lodash with all posibilities that ES6 already has.

https://www.sitepoint.com/lodash-features-replace-es6/

But if you want to use it, try to import it in this way:

import * as _ from "lodash";

import * as _ from "lodash"; Please note that If you use import _ from “lodash”, you will receive following error that “Module ‘lodash’ has no default export”:

From https://hassantariqblog.wordpress.com/2016/10/15/angular2-import-lodash-into-angular2-application-using-typescript/

Or for just one function of lodash:

import wrapperLodash from 'lodash/wrapperLodash';
Sign up to request clarification or add additional context in comments.

7 Comments

Unfortunately, this is to import the whole library. I need to import only particular function and need to do it globally (only once).
Try this 'import wrapperLodash from 'lodash/wrapperLodash';'
'_' refers to a UMD global, but the current file is a module. Consider adding an import instead.
exactly... and there is no aswer to this topic.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.