最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - React Module not found: Can't resolve '..utilsapi' - Stack Overflow

programmeradmin2浏览0评论

I created a module in which I keep functions to call an 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?

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.

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

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

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)
      })
  },
}

I created a module in which I keep functions to call an 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?

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.

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

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

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)
      })
  },
}
Share Improve this question asked May 31, 2018 at 10:23 Rik SchoonbeekRik Schoonbeek 4,5204 gold badges29 silver badges47 bronze badges 1
  • 2 You need go up two directories. ../ (Out of search dir) and '../ (Out of Components) – Siya Mzam Commented May 31, 2018 at 10:25
Add a ment  | 

1 Answer 1

Reset to default 6

You have to go two directories up like below

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

it will work.

发布评论

评论列表(0)

  1. 暂无评论