0

I'm getting this error in my react project

./src/components/HomePage.js
Module not found: Can't resolve 'axios' in 'C:\Users\Admin\Desktop\fcwShp-ft\frontend\src\components'

My code:

import React, { useState, useEffect } from 'react';
import { Link } from 'react-router-dom';
import axios from 'axios';

const HomePage = () => {

    const [products, setProduct] = useSatate([]);

    useEffect(() => {
        const fetchData = async () => {
            const { data } = await axios.get("/api/products");
            setProduct(data);
        }
        return () => {

        };
    }, [])

    return (
        <ul className="products">
            {
                products.map(product =>
                    <li className="liProp">
                        <div className="product">
                            <Link to={"/product/" + product._id}>
                                <img className="product-image" src={product.image} alt="product" />
                            </Link>
                            <div className="product-name">
                                <Link to={"/product/" + product._id}>{product.name}</Link>
                            </div>

                            <div className="product-brand">{product.brand}</div>

                            <div className="product-price">$ {product.price}</div>

                            <div className="product-rating">{product.rating} Stars ({product.numReviews} Reviews)</div>
                        </div>
                    </li>
                )
            }
        </ul>
    )
}

export default HomePage;

Maybe someone has solved the same problem (i hope) and can help?

Also tried answers in this thread but nothing helped

Click

Was trying npm install axios && npm install --save axios

anthonyro
  • 99
  • 1
  • 11
  • 2
    Did you stop your server before installing and restart it afterwards? The hot reload might not have worked – Halmon Nov 07 '20 at 19:18
  • It tries to find `axios` in a local directory for some reason. I think you should look at the configuration that may affect module resolution (in webpack config I guess). – Sergey Nov 07 '20 at 19:19

1 Answers1

0

Solved this by using yarn add

instead of npm install

anthonyro
  • 99
  • 1
  • 11