164

I am using react for my application. I have a div that I would like to have a background image. But I can't get it to show.

When I include it in the src folder as myapp/src/bgimage.png it works perfectly but I've heard that I should include it in a folder named images at the root level so it's myapp/images/bgimage.png, however this does not work for me and gives me:

You attempted to import ../images/bgimage.png which falls outside of the project src/ directory.'

Can anyone tell me the proper way to include image assets in reactJS?

3
  • 1
    Are u using webpack or other bundler? Commented Jun 20, 2017 at 2:58
  • 2
    @RickLee I think he's using create-react-app that's how I encountered the same error. And it's using webpack 2. Commented Jun 20, 2017 at 3:05
  • 2
    You should read this answer it's a similar issue to yours. I've encountered the same issue as well, what I did was just put the assets folder inside the src directory. Commented Jun 20, 2017 at 3:10

9 Answers 9

200

public: anything that is not used by your app when it compiles

src: anything that is used when the app is compiled

So for example if you use an image inside a component, it should be in the src folder but if you have an image outside the app (i.e. favicon) it should be in public.

Sign up to request clarification or add additional context in comments.

7 Comments

what does it mean by "not used by your app"?
@Fadhil guess it means not of actual usage in any React component or in stylesheets
@Fadhil not required for the app to compile
I don't think it's correct to say that a favicon is "outside the app", although I can understand what you mean by that. Is there any other example besides the favicon that should be in public? Could you explain why?
to clarify, you put static assets that are used by index.html into the public folder, and you manage other static assets that are accessed by your components in src/assets folder.
|
61

I would add that creating an "assets" folder inside the "src" folder is a good practice.

1 Comment

Very unhelpful answer. It may be common practice, but why is it good practice?
33

Use /src if you are using create-react-app


If you are using create-react-app, You need to use /src for the following benefits.

  1. Scripts and stylesheets get minified and bundled together to avoid extra network requests.
  2. Missing files cause compilation errors instead of 404 errors for your users.
  3. Result filenames include content hashes so you don’t need to worry about browsers caching their old versions.

Also, if you are using webpack's asset bundling anyway, then your files in /src will be rebuilt.

You may create subdirectories inside src. For faster rebuilds, only files inside src are processed by webpack. You need to put any JS and CSS files inside src, otherwise webpack won’t see them.

See this link

3 Comments

What if the ReactJc project houses 20.000 images? And also users can download images!
I think storing 20,000 into project is not good idea. Use remote storage and download and use it
I know about remote storage the question is how to do it in one of the assets-in-public-or-src
13

According to the create-react-app documentation, regarding the use of the public folder:

Normally we recommend importing stylesheets, images, and fonts from JavaScript. The public folder is useful as a workaround for a number of less common cases:

  • You need a file with a specific name in the build output, such as manifest.webmanifest.
  • You have thousands of images and need to dynamically reference their paths.
  • You want to include a small script like pace.js outside of the bundled code.
  • Some libraries may be incompatible with webpack and you have no other option but to include it as a tag.

Comments

11

No,

public folder is for static file like index.html and ...

I think you should make an "assets" folder in src folder and access them in this way.

2 Comments

Why would you say an image is not static?
This answer makes no sense whatsoever, I don't get how it has 15 upvotes...
10

In this article, I mentioned that

Keep an assets folder that contains top-level CSS, images, and font files.

In react best practices we keep an assets folder inside the src which may contain top-level CSS, images, and font files.

Comments

8

In continuation with the other answers I would further like to add that you should create an 'assets' folder under 'src' folder and then create 'images' folder under 'assets' folder. You can store your images in the 'images' folder and then access them from there.

Comments

7

Difference between public and src/assets folders:

  • public folder is for anything that is being imported into your index.html
  • src/assets folder is for anything that is imported in your js/ts/jsx/tsx/...etc... files that are in the src folder.

Example of referring files that are in public folder

<!-- index.html -->
<head>
  <link rel="manifest" href="/manifest.json">
  <link rel="icon" type="image/svg+xml" href="/favicon.svg" />
</head>

Example of referring files that are in the src/assets folder

// src/App.tsx

import Logo from "./assets/logo.svg"

export default () => {

   return <img src={Logo} /> 
}

Comments

1

As per my understanding I will go with easier way. If you use your assets from public folder, after build contents from public will be maintained as same. So, if you deploy your app, the contents from public folder will also be loaded while your app loads. Assume your build is 5 MB (4 MB assets and 1 MB src) the 4 MB will get downloaded first then follows the src contains. Even if you use lazy and suspense your app will be slow during deployment.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.