简体   繁体   English

未找到React模块:无法解析'../utils/api'

[英]React Module not found: Can't resolve '../utils/api'

I created a module in which I keep functions to call an api. 我创建了一个模块,在其中保留了调用api的函数。 While 'requiring' it, I get the following error: 在“要求”它时,出现以下错误:

./src/Components/Search/SearchPage.js
Module not found: Can't resolve '../utils/api' in 'C:\Users\riksch\Dropbox\projects\Current\greenmp\frontend\src\Components\Search'

My main question is: how do I correctly import the api module into SearchPage.js? 我的主要问题是:如何正确将api模块导入SearchPage.js?

Here is the structure of my project: 这是我的项目的结构:

在此处输入图片说明

I've highlighted the files that I use, 1 is the file that I import (require) from, and 2 is the module I try to import. 我突出显示了我使用的文件,1是我从中导入(要求)的文件,2是我尝试导入的模块。

This worked before, but now that I changed the folder structure, even after adjusting the path, I can't get it too work. 以前可以使用,但是现在更改了文件夹结构,即使调整了路径,也无法正常工作。

I've tried different import paths, all with the same error. 我尝试了不同的导入路径,但都具有相同的错误。

SearchPage.js require statement SearchPage.js require语句

const api = require('../utils/api')

api.js api.js

var axios = require('axios')


module.exports = {
  retrievePlants: function(search_query, locale) {
    console.log("api.retrievePlants executes")
    console.log("url: " + 'http://127.0.0.1:8000/search/'+locale+'/'+search_query)
    //FIXME: hardcoded URL HOST
    // return axios.get('https://127.0.0.1/search/'+locale+'/'+search_query)
    return axios.get('http://127.0.0.1:8000/search/'+locale+'/'+search_query)
      .then(function(response) {
        console.log("response.data:")
        console.log(response.data)
        return response.data
      })
      .catch(function(error) {
        console.log("Error in Components.utils.api.retrievePlants:")
        console.log(error)
        console.log("console.log(error.response.data):")
        console.log(error.response.data)
      })
  },
}

You have to go two directories up like below 您必须像下面一样进入两个目录

const api = require('../../utils/api');

it will work. 它会工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM