The create-react-app imports restriction outside of src directory

前端 未结 17 1810
时光说笑
时光说笑 2020-11-22 13:20

I am using create-react-app. I am trying to call an image from my public folder from a file inside my src/components. I am receiving this error message.

17条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-22 13:49

    To offer a little bit more information to other's answers. You have two options regarding how to deliver the .png file to the user. The file structure should conform to the method you choose. The two options are:

    1. Use the module system (import x from y) provided with react-create-app and bundle it with your JS. Place the image inside the src folder.

    2. Serve it from the public folder and let Node serve the file. create-react-app also apparently comes with an environment variable e.g. ;. This means you can reference it in your React app but still have it served through Node, with your browser asking for it separately in a normal GET request.

    Source: create-react-app

提交回复
热议问题