【问题标题】:Module not found: Can't resolve '...' [duplicate]找不到模块:无法解析“...” [重复]
【发布时间】:2019-07-17 17:44:40
【问题描述】:

尝试导入我的函数后,出现错误:

找不到模块:无法解析“C:\Users\lesha\Desktop\blog\src\components\HomePage”中的“fetchProducts”

我的层次结构的屏幕:

不知道问题出在哪里

主页.jsx

import React, { Component } from "react";
import "./HomePage.css";
import { connect } from "react-redux";
import { bindActionCreators } from "redux";
import fetchProductsAction from "fetchProducts";
import { fetchProducts } from "../fetchMovies/fetchMovies.js";
class HomePage extends Component {
  componentDidMount() {
    fetchProducts();
  }

  render() {
    return <div>asd</div>;
  }
}

const mapStateToProps = state => {
  return {};
};
const mapDispatchToProps = dispatch =>
  bindActionCreators(
    {
      fetchProducts: fetchProductsAction
    },
    dispatch
  );

export default connect(
  mapStateToProps,
  mapDispatchToProps
)(HomePage);

fetchMovies.js

import {
  fetchProductsPending,
  fetchProductsSuccess,
  fetchProductsError
} from "../../actions/index";

export function fetchProducts() {
  return dispatch => {
    dispatch(fetchProductsPending());
    fetch(
      "https://api.themoviedb.org/3/movie/top_rated?api_key=b05433b24b52d30bb0b22ecfd4f86001&language=en-US&page=1"
    )
      .then(res => res.json())
      .then(res => {
        if (res.error) {
          throw res.error;
        }
        dispatch(fetchProductsSuccess(res.products));
        console.log(res.products);
        return res.products;
      })
      .catch(error => {
        dispatch(fetchProductsError(error));
      });
  };
}

【问题讨论】:

  • 需要明确说明fetchProducts的路径,如import fetchProductsAction from "./fetchProducts"
  • 在 Stack Overflow 上提问之前,请至少搜索错误消息的文本。

标签: javascript reactjs redux


【解决方案1】:

就是这一行:

import fetchProductsAction from "fetchProducts";

节点尝试加载一个名为“fetchProducts”的模块,该模块可能找不到。

您可能想找出名为fetchProducts 的东西在哪里声明并从那里加载。

【讨论】:

    【解决方案2】:
    1. 在根文件夹中创建 .env 文件

    2. NODE_PATH=./src 放入您的 .env 文件中

    3. 现在试试,import { fetchProducts } from "../fetchMovies/fetchMovies.js";

    【讨论】:

      猜你喜欢
      • 2017-10-23
      • 2021-10-16
      • 2021-08-16
      • 2019-05-10
      • 2021-10-17
      • 2021-11-01
      • 2018-04-20
      • 2021-11-30
      • 1970-01-01
      相关资源
      最近更新 更多