Search code examples
javascriptlodashstenciljs

Using lodash with stenciljs


I am trying to import lodash into my stenciljs component. I have tried multiple ways and viewed many solutions regarding this but got no luck on this.

I have tried to import in the following ways:

import * as _ from 'lodash';
import _ from 'lodash';
import 'lodash';
import {_} from 'lodash';

None of them is working for me. First one does not throw any error but I am not able to use any function from lodash. Rest of the ways throw errors and the build fails.

Any help would be really appreciable.

Thanks in advance!!


Solution

  • Your import is fine. Stencil compiler is resolving your lodash function call as "undefined". Try to add lodash types in your project. It should resolve the function definitions at compile time.

    yarn add -D  lodash @types/lodash
    
    OR
    
    npm install --save-dev lodash @types/lodash