How to import file from outside /components in react

前端 未结 3 1030
情书的邮戳
情书的邮戳 2021-01-22 02:03

My folder structure is as follows:

In my App.Js (which is under the components folder), I have:

import variables from \'/src/EnvVariables/varia         


        
相关标签:
3条回答
  • 2021-01-22 02:23

    Have you tried to import with the relative path? (with one or more ../ sections)

    import variables from "../EnvVariables/variables.json"
    

    Of course, if you prefer absolute paths you can setup https://webpack.js.org/configuration/resolve/, but I believe for first, you should try the first solution

    0 讨论(0)
  • 2021-01-22 02:28

    if you just need to import your any file to a component : (just follow this steps)

    1. Simply you can create a .env file in src directory .
    2. In that .env file you need to add this line only NODE_PATH = src/
    3. Then you can write import "Home.css" (your file name will be here) to a component from src, it will take it easily.
    4. Run the program again , then you will get the same error, but take it easy and just you have to do fully start your program with the npm start from a new terminal.
    0 讨论(0)
  • 2021-01-22 02:31

    Do ../../ before path

    source={require('../../assets/eating.png')}
    
    0 讨论(0)
提交回复
热议问题