Module not found: Error: Cannot resolve module '@types/lodash'

20.8k views Asked by At

I'm stuck and looking for help.

I'm not sure where the problem actually is but I hope to find the answer.

Problem details

I've installed '@types/lodash' using following command:

npm install --save-dev @types/lodash

And imported lodash in my .ts file using:

import * as _ from '@types/lodash';

When I run webpack I get following error:

ERROR in ./app/scripts/Components/GameBoard.ts
Module not found: Error: Cannot resolve module '@types/lodash' in /Users/mjelaska/Documents/DevProjects/connect-four/src/app/scripts/Components
 @ ./app/scripts/Components/GameBoard.ts 3:8-32

Then I tried to get more details with

webpack --display-error-details

and got ⇣

ERROR in ./app/scripts/Components/GameBoard.ts
Module not found: Error: Cannot resolve module '@types/lodash' in /Users/mjelaska/Documents/DevProjects/connect-four/src/app/scripts/Components
resolve module @types/lodash in /Users/mjelaska/Documents/DevProjects/connect-four/src/app/scripts/Components
  looking for modules in /Users/mjelaska/Documents/DevProjects/connect-four/src/node_modules
    resolve 'file' or 'directory' lodash in /Users/mjelaska/Documents/DevProjects/connect-four/src/node_modules/@types
      resolve file
        /Users/mjelaska/Documents/DevProjects/connect-four/src/node_modules/@types/lodash.webpack.js doesn't exist
        /Users/mjelaska/Documents/DevProjects/connect-four/src/node_modules/@types/lodash is not a file
        /Users/mjelaska/Documents/DevProjects/connect-four/src/node_modules/@types/lodash.web.js doesn't exist
        /Users/mjelaska/Documents/DevProjects/connect-four/src/node_modules/@types/lodash.ts doesn't exist
        /Users/mjelaska/Documents/DevProjects/connect-four/src/node_modules/@types/lodash.js doesn't exist
      resolve directory
        directory default file index
          resolve file index in /Users/mjelaska/Documents/DevProjects/connect-four/src/node_modules/@types/lodash
            /Users/mjelaska/Documents/DevProjects/connect-four/src/node_modules/@types/lodash/index doesn't exist
            /Users/mjelaska/Documents/DevProjects/connect-four/src/node_modules/@types/lodash/index.webpack.js doesn't exist
            /Users/mjelaska/Documents/DevProjects/connect-four/src/node_modules/@types/lodash/index.web.js doesn't exist
            /Users/mjelaska/Documents/DevProjects/connect-four/src/node_modules/@types/lodash/index.ts doesn't exist
            /Users/mjelaska/Documents/DevProjects/connect-four/src/node_modules/@types/lodash/index.js doesn't exist
        use  from package.json
          resolve 'file' or 'directory' /Users/mjelaska/Documents/DevProjects/connect-four/src/node_modules/@types/lodash
            resolve file
              /Users/mjelaska/Documents/DevProjects/connect-four/src/node_modules/@types/lodash is not a file
              /Users/mjelaska/Documents/DevProjects/connect-four/src/node_modules/@types/lodash.webpack.js doesn't exist
              /Users/mjelaska/Documents/DevProjects/connect-four/src/node_modules/@types/lodash.web.js doesn't exist
              /Users/mjelaska/Documents/DevProjects/connect-four/src/node_modules/@types/lodash.ts doesn't exist
              /Users/mjelaska/Documents/DevProjects/connect-four/src/node_modules/@types/lodash.js doesn't exist
            resolve directory
              use  from package.json
                abort resolving because of recursion
              directory default file index
                resolve file index in /Users/mjelaska/Documents/DevProjects/connect-four/src/node_modules/@types/lodash
                  /Users/mjelaska/Documents/DevProjects/connect-four/src/node_modules/@types/lodash/index doesn't exist
                  /Users/mjelaska/Documents/DevProjects/connect-four/src/node_modules/@types/lodash/index.webpack.js doesn't exist
                  /Users/mjelaska/Documents/DevProjects/connect-four/src/node_modules/@types/lodash/index.web.js doesn't exist
                  /Users/mjelaska/Documents/DevProjects/connect-four/src/node_modules/@types/lodash/index.ts doesn't exist
                  /Users/mjelaska/Documents/DevProjects/connect-four/src/node_modules/@types/lodash/index.js doesn't exist
[/Users/mjelaska/Documents/DevProjects/connect-four/src/node_modules/@types/lodash.webpack.js]
[/Users/mjelaska/Documents/DevProjects/connect-four/src/node_modules/@types/lodash.web.js]
[/Users/mjelaska/Documents/DevProjects/connect-four/src/node_modules/@types/lodash.ts]
[/Users/mjelaska/Documents/DevProjects/connect-four/src/node_modules/@types/lodash.js]
[/Users/mjelaska/Documents/DevProjects/connect-four/src/node_modules/@types/lodash/index]
[/Users/mjelaska/Documents/DevProjects/connect-four/src/node_modules/@types/lodash/index.webpack.js]
[/Users/mjelaska/Documents/DevProjects/connect-four/src/node_modules/@types/lodash/index.web.js]
[/Users/mjelaska/Documents/DevProjects/connect-four/src/node_modules/@types/lodash/index.ts]
[/Users/mjelaska/Documents/DevProjects/connect-four/src/node_modules/@types/lodash/index.js]
[/Users/mjelaska/Documents/DevProjects/connect-four/src/node_modules/@types/lodash.webpack.js]
[/Users/mjelaska/Documents/DevProjects/connect-four/src/node_modules/@types/lodash.web.js]
[/Users/mjelaska/Documents/DevProjects/connect-four/src/node_modules/@types/lodash.ts]
[/Users/mjelaska/Documents/DevProjects/connect-four/src/node_modules/@types/lodash.js]
[/Users/mjelaska/Documents/DevProjects/connect-four/src/node_modules/@types/lodash/index]
[/Users/mjelaska/Documents/DevProjects/connect-four/src/node_modules/@types/lodash/index.webpack.js]
[/Users/mjelaska/Documents/DevProjects/connect-four/src/node_modules/@types/lodash/index.web.js]
[/Users/mjelaska/Documents/DevProjects/connect-four/src/node_modules/@types/lodash/index.ts]
[/Users/mjelaska/Documents/DevProjects/connect-four/src/node_modules/@types/lodash/index.js]
 @ ./app/scripts/Components/GameBoard.ts 3:8-32

Setup details

package.json (w/o some irrelevant info)

{
  "devDependencies": {
    "@types/lodash": "^4.14.47",
    "@types/node": "^6.0.55",
    "@types/pixi.js": "^4.3.1",
    "npm": "^4.0.5",
    "ts-loader": "^1.3.3",
    "typescript": "^2.1.4",
    "webpack": "^1.14.0"
  }
}

tsconfig.json

{
    "compilerOptions": {
        "module": "commonjs",
        "target": "es5",
        "noImplicitAny": true,
        "sourceMap": false,
        "removeComments": true,
        "typeRoots": [
          "./node_modules/@types"
        ]
    },
    "files": [
      "./app/scripts/main.ts"
    ]
}

webpack.config.js

module.exports = {
    entry: './app/scripts/main.ts',
    output: {
        filename: './build/scripts/main.js'
    },
    resolve: {
        extensions: ['', '.webpack.js', '.web.js', '.ts', '.js']
    },
    module: {
        loaders: [
            {
                test: /\.ts$/,
                loader: 'ts-loader'
            }
        ]
    }
};

Environment details

node: v6.9.4
npm: v4.0.5
webpack: v1.14.0 (as seen in `package.json`, installed also globally)

Project at the moment when problem occurred: https://github.com/MiroslavJelaska/connect-four/tree/4941ca792096602831940f76fa350e461808a8c5

⬆️ Update #1

Installing lodash to dependencies instead of devDependencies doesn't help:

npm install --save @types/lodash
2

There are 2 answers

2
JKillian On BEST ANSWER

You need the actual lodash package (which contains the JS code that is lodash) and you also need the @types/lodash package, which provides type definitions for the lodash package (essentially, it tells TypeScript what's in the lodash package).

So, you want to run both of these commands to get those two packages:

npm install --save-dev @types/lodash
npm install --save lodash

Then in your code, you import it as such:

import * as _ from 'lodash';

_.zip(array1, array1); // or whatever
0
Venkat On

For me declare module "*.lodash"; in *.d.ts worked