Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Module not found: Can't resolve 'styled-component' [closed]

I created a new react app using create-react-app and then installed styled-component using npm. But when I use it in component, I am getting following error.

Failed to compile. ./src/Components/Lightbox/styledLightbox.js

Module not found:

Can't resolve 'styled-component' in '/Users/ishan/Documents/react-app/src/Components/Lightbox'

Following is the component:

import React, { Component } from 'react';
import { LightboxWrapper } from './styledLightbox';

class Lightbox extends Component {

    renderEntry() {
        if (this.props.lightbox.type === "photo") {
         return  <img alt="name" src={this.props.lightbox.entry} />;
        } 
        if (this.props.lightbox.type === "video") {
        return <video src={this.props.lightbox.entry} onLoadedMetadata={(e) => {console.log("Video duration is: "+e.currentTarget.duration) } } autoPlay muted onEnded={() => console.log("Video ended")} width='100%' height='100%' ></video> 
        }
        if(this.props.lightbox.type === "text"){
        return <div> <p className="lightbox text"> {this.props.lightbox.entry} </p> </div>   
        }

    }

    render() {
        let classList = this.props.lightbox.status === true ? "lightbox-wrapper active" : "lightbox-wrapper";
        return ( 
                <LightboxWrapper className={classList}>
                    {/* {this.renderEntry()} */}
                </LightboxWrapper>
         );
    }
}

export default Lightbox;

Following is the styled-component created to use it in other component:

import styled from "styled-component";

export const LightboxWrapper = styled.div`
      display: none;
        position: fixed;
        z-index: 100;
        left: 0;
        top: 0;
        width: 100%;
        height: 100%;
        overflow: auto;
        background: radial-gradient(
                center, 
                ellipse farthest-corner, 
                rgba(255,255,255,0) 0%,
                rgba(255,255,255,0) 100%
        );
    .active {
        display: block;
        background: radial-gradient(
            center, 
            ellipse farthest-corner, 
            rgba(255,255,255,0.5) 0%,
            rgba(255,255,255,0.1) 100%
    );
    }
    .active img, .active video {
        max-width: 70%;
        height: auto !important;
        margin: 0 auto;
        opacity: 1;
        /* box-shadow: 0px 2px 7px rgba(0,0,0,0.2); */
        transition: opacity 0.5s linear;
        animation: fadeInScale 1.2s ease-in-out;
        max-height: 70%;
        width: auto !important;
        position: relative;
        top: 11%;
}
`;
like image 542
Ishan Patel Avatar asked Mar 02 '19 17:03

Ishan Patel


People also ask

How do I fix module not found?

Make sure that your file's letter-casing correctly matches with its respective static imports. Also make sure that your letter-casing in your filenames and imports are identical between your repository and local machine. If you are using git , make sure git config core. ignorecase false is set in your environment.

What problem does styled-components solve?

Advantages of using Styled-components Eliminates class name bugs: styled-components provide unique class names for your styles, thus eliminating the problems with class names duplications, misspellings, and overlaps.

How do I use styled JSX?

styled-jsx v3 comes with a webpack loader that lets you write styles in regular css files and consume them in React. To consume the styles in your component you can import them from your CSS file and render them using a <style jsx> tag. Remember to add the global prop if you want your styles to be global.


1 Answers

You are missing the "s" at the end of "styled-components"

$ npm i styled-components
like image 75
Hemadri Dasari Avatar answered Sep 18 '22 05:09

Hemadri Dasari