0

I'm new to lwc and I'm trying to import an external library - lodash.
I tried some ways and none of them worked for me. I'm probably doing something wrong

I didn't find any good tutorial that shows how to upload and use lodash in lwc.
My questions are:

  1. What exactly should I download?
  2. How to upload it to salesforce? as a static resource? or just a folder in my project?
  3. How to import it and use its functions?

Thanks for any help.

5
  • 2
    I personally didn't try it, but here is an example, how to do it LWC Lodash Commented Nov 1, 2021 at 14:29
  • I'm writing an answer, by the way, if you give me a few minutes.
    – sfdcfox
    Commented Nov 1, 2021 at 14:30
  • Given that LWC supports and polyfills most of the modern JavaScript features, I suggest you wait for a while before introducing 3rd party libraries. If you run into cases where lodash "pays for itself" i.e. its benefits outweigh the learning curve for future developers then add it at that point.
    – Keith C
    Commented Nov 1, 2021 at 14:39
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking.
    – Community Bot
    Commented Nov 1, 2021 at 14:39
  • @OleksandrBerehovskyi That's a cool library, you can import individual features, but if I were to use lodash, I'd import it in a way that allows it to be used exactly as the docs describe (not that there's nothing wrong with decomposed functions).
    – sfdcfox
    Commented Nov 1, 2021 at 14:41

1 Answer 1

9

What exactly should I download?

https://raw.githubusercontent.com/lodash/lodash/4.17.15-npm/core.min.js

Or

https://raw.githubusercontent.com/lodash/lodash/4.17.15-npm/lodash.min.js

Depending on what features you need.

How to upload it to salesforce? as a static resource? or just a folder in my project?

You can either choose to use the static resource method:

import LODASH_RESOURCE from '@salesforce/resourceUrl/lodash'
import { loadScript } from 'lightning/platformResourceLoader'
import { LightningElement } from 'lwc'

export default class MyComponentName extends LightningElement {
  _lodashLoaded
  async connectedCallback() {
    await loadScript(this, LODASH_RESOURCE)
    this._lodashLoaded = true
  }
}

Or you can create a new LWC, delete the HTML file, and in the JS file, copy-paste either script from above into the file, and add one final line to your code:

export default _;

This last line allows you to import lodash directly:

import _ from 'c/lodash'

Note that you can't use the uncompressed versions, as files must not exceed 128KiB file size.

How to import it and use its functions?

Importing is discussed above. As for using it, you use it as you would without LWC:

this.users = _.sortBy(this.users, ['user','age'])

(This example is basically a modified example from the docs).


Here's a demo for you on webcomponents.dev.

3
  • 2
    I have used the first method a couple of times (i.e. loadScript) and it works as expected. Don't know which was is more performant Commented Nov 1, 2021 at 14:44
  • 1
    @BryanAnderson The latter causes the script to be loaded with the component, so no waiting on async calls to the server, and less code overall, but the former only pays the round-trip bandwidth once, so it's really a matter of preference. I would prefer the import statement myself, because I don't have to load the platformResourceLoader plus call a loadScript, but I do like to present both methods, as there might be a reason why one might choose the loadScript version (e.g. they want to load a debug version, which exceeds the 128KiB file limit).
    – sfdcfox
    Commented Nov 1, 2021 at 14:50
  • Thank you! I tried these ways but every time I uploaded something else and it didn't work. You made it clear for me now. Commented Nov 1, 2021 at 20:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .