找不到模块:无法解析“ ...”

时间:2019-07-17 17:44:40

标签: javascript reactjs redux

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

  

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

我的阶层的屏幕:

enter image description here

我不知道问题出在哪里

HomePage.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));
      });
  };
}

2 个答案:

答案 0 :(得分:0)

是这一行:

import fetchProductsAction from "fetchProducts";

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

您可能想找出声明为fetchProducts的内容并从那里加载。

答案 1 :(得分:0)

  1. 在您的根文件夹中创建.env文件

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

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