42

I am getting the following error:

node_modules/@types/react-redux/index.d.ts(8,24): error TS2307: Cannot find module 'redux'.

despite having installed both react-redux and redux (package.json):

"dependencies": {
    "react": "15.4.2",
    "react-native": "0.42.3",
    "react-redux": "^5.0.3",
    "redux": "^3.6.0"
},
"devDependencies": {
    "@types/react": "^15.0.18",
    "@types/react-native": "^0.42.9",
    "@types/react-redux": "^4.4.38",
    "@types/redux": "^3.6.0",
    "babel-jest": "19.0.0",
    "babel-preset-react-native": "1.9.1",
    "jest": "19.0.2",
    "react-test-renderer": "15.4.2",
    "tslint": "^4.5.1",
    "typescript": "^2.2.1"
},

The README.md file in @types/redux says:

This is a stub types definition for Redux (https://github.com/reactjs/redux).
Redux provides its own type definitions, so you don't need @types/redux installed!

but uninstalling the @types/redux package makes no difference.

Any ideas?

UPDATE

I thought that by adding an index.d.ts to the @types/redux directory (containing only export * from ../../redux/index), it would solve the problem, but no joy.

Here is my tsconfig.json:

{
    "compilerOptions": {
        "target": "es2015",
        "module": "es2015",
        "jsx": "react",
        "outDir": "build",
        "rootDir": "src",
        "allowSyntheticDefaultImports": true,
        "noImplicitAny": true,
        "experimentalDecorators": true,
        "preserveConstEnums": true,
        "allowJs": true,
        "sourceMap": true
    },
    "filesGlob": [
        "src/**/*.ts",
        "src/**/*.tsx"
    ],
    "exclude": [
        "__tests__",
        "index.android.js",
        "index.ios.js",
        "build",
        "node_modules"
    ],
    "compileOnSave": false
}

and I definitely deleted the @types/redux directory from node_modules. I am (obviously) running TypeScript 2.2.1.

UPDATE 2

Code using redux:

import { combineReducers } from "redux"

export default combineReducers({})

Note that adding a semicolon after "redux" does not help.

5
  • Yes, it should work without @types/redux. Actually, I think I had problems, when I had @types/redux installed... Could you provide a piece of code in which you're importing redux? Commented Mar 24, 2017 at 17:25
  • Just to be sure, put here your tsconfig.json as well. Commented Mar 24, 2017 at 17:29
  • 1
    import { createStore, Store, Reducer } from 'redux' this works just fine in typescript 2.3.0-dev.20170320 Commented Mar 24, 2017 at 17:30
  • @Ralph make sure that @types/redux is really deleted from your node_modules directory and not just removed from your package.json Commented Mar 24, 2017 at 17:56
  • @Erik: Reverting to @types/[email protected] fixed the problem. I suspect an issue with the TypeScript compiler. Commented Mar 24, 2017 at 20:00

7 Answers 7

87

I experienced the same problem on a react-native project. This fix worked for me:

You have to add "moduleResolution": "node" to the compiler options in order to include type definitions linked in package.json files.

Documentation here https://www.typescriptlang.org/docs/handbook/module-resolution.html

Sign up to request clarification or add additional context in comments.

2 Comments

I'd guess you'd be forgiven for thinking that, when they say "Nowadays, this strategy is mainly present for backward compatibility." in their official docs, classic wouldn't be the default... This just solved me a whole lot of pain, thanks
That solved my issue as well. FTFM "There are two possible module resolution strategies: Node and Classic (...) If not specified, the default is Classic for --module AMD | System | ES2015 or Node otherwise." typescriptlang.org/docs/handbook/module-resolution.html
7

I reverted to @types/[email protected] and that resolved the problem. This version includes the index.d.ts file for redux. There might be a problem with the TypeScript compiler not looking for the index.d.ts file in the normal node_modules/redux directory, but only under node_module/@types.

1 Comment

forward 2022 this issue with that version is still presente but reverting to '@types/[email protected]?' didn't fixed it
3

If you encounter the same issue with the following react version "^16.8.6" verify if the npm redux package

Package.json

"dependencies": {
    "@types/jest": "24.0.13",
    "@types/node": "12.0.3",
    "@types/react": "16.8.19",
    "@types/react-dom": "16.8.4",
    "react": "^16.8.6",
    "react-dom": "^16.8.6",
    "react-redux": "^7.0.3",
    "react-scripts": "3.0.1",
    "redux": "^4.0.1",
    "typesafe-actions": "^4.4.0",
    "typescript": "3.5.1"
  },

if not installed just do npm install redux no need for npm i @types/reduxas Redux package provides its own type definitions, so you don't need @types/redux installed!

Comments

2

I already had moduleResolution: 'node' and @types/redux was deleted. But when I deleted node_modules and reinstalled, the problem went away. :shrug.

Comments

0

Faced the same problem.

Fixed with re-installing redux and save it.

npm install redux --save

Comments

0

I had the same problem after installing the redux toolkit. Interestingly, I did not have to re-install node modules. What worked for me is stopping and re-starting the live server -- problem gone!

  1. on mac, on your VScode terminal, press "Control + C" to terminate the server
  2. Run "npm start" again

Comments

0

For anyone who has the same issue,try following:

  • Restarting vscode
  • Deleting and reinstalling node_modules
  • Restarting your PC(worked for me).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.