Search code examples
javascriptnode.jstypescriptwebpacklodash

Lodash-es with Typescript and Webpack module error


I want to use lodash-es to be able to import only the functions I need, but I get an error.

import { camelCase } from "lodash-es";
//...
console.log(camelCase('Foo'));

Error:

Module not found: Error: Can't resolve 'lodash-es' in '/Users/...

Browser console error

Uncaught Error: Cannot find module 'lodash-es' at webpackMissingModule..

My package.json

...
"devDependencies": {
"@types/lodash": "^4.14.178",
"@types/lodash-es": "^4.17.5",

Using normal lodash I have no problems, but the library is too big for my purpose


Solution

  • You also need lodash-es (or lodash) in your dependecies. You can import individual functions like this:

    import camelCase from "lodash-es/camelCase";
    

    to keep the bundle size low. @types/* packages are just type definitions.

    Also, some functions can be installed isolated from the rest: https://www.npmjs.com/package/lodash.camelcase