【问题标题】:Module not found: Can't resolve 'axios' in 'C:\Users\Admin\Desktop\MyApp\frontend\src\components'找不到模块:无法解析“C:\Users\Admin\Desktop\MyApp\frontend\src\components”中的“axios”
【发布时间】:2020-11-08 15:05:02
【问题描述】:

我在我的 react 项目中遇到了这个错误

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

我的代码:

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;

也许有人已经解决了同样的问题(我希望)并且可以提供帮助?

也尝试了这个帖子中的答案,但没有任何帮助

Click

正在尝试 npm install axios && npm install --save axios

【问题讨论】:

  • 您是否在安装之前停止了服务器并在之后重新启动它?热重载可能不起作用
  • 它出于某种原因试图在本地目录中找到axios。我认为您应该查看可能影响模块分辨率的配置(我猜是在 webpack 配置中)。

标签: reactjs axios


【解决方案1】:

通过使用 yarn add

解决了这个问题

而不是 npm 安装

【讨论】:

    猜你喜欢
    • 2021-07-15
    • 1970-01-01
    • 1970-01-01
    • 2018-11-19
    • 2021-11-24
    • 2020-11-06
    • 2021-06-06
    • 1970-01-01
    • 2021-10-17
    相关资源
    最近更新 更多