Search code examples
reactjstypescriptnpmstyled-components

TypeScript React error: "Module not found: Can't resolve 'styled-components'"


After installing @types/styled-components package and compiling my Typescript React app I keep getting error:

Module not found: Can't resolve 'styled-components'

I have dependancy in both package.json and package-lock.json and my import looks like this:

import styled from 'styled-components';

I even have the auto-completion when importing and yet after compiling, aforementioned error keeps popping up. What may be causing the problem and how to fix it?


Solution

  • You just installed the types for the library, not the library itself. That explains the autocompletion as that comes from the type declaration files (.d.ts). You should run npm i styled-components as well.