How to use files in public folder in ReactJS ?
Last Updated :
10 Apr, 2025
In React, the files store public folder contains static files such as index.html, javascript library files, images, and other assets, etc. which you don't want to be processed by Webpack. Files in this folder are copied and pasted as they are directly into the build folder. Files inside the `public` folder can only be referenced from the HTML.
Understanding the Public Folder
When you create a React app using create-react-app, a public folder is automatically generated. This folder is where you store static files that should be publicly accessible.
The typical contents of the public folder include:
- index.html: The template for your app's main HTML file.
- favicon.ico: The small icon that appears on the browser tab.
- Other static files like images, PDFs, or custom stylesheets.
Steps to Create a React Application
Step 1: Create a React application using the following command:
npx create-react-app foldername
Step 2: After creating your project folder i.e. foldername, move to it using the following command:
cd foldername
Project Structure:
The project structure will look like the following after creating the assets folder with gfg.png file sample. Also, we have kept the gfgPublic.png file in the public folder.
Simple react app folder structure
JavaScript
// App.js file
import Intro from './components/Intro';
import './App.css';
function App() {
return (
<div className="App">
<Intro />
</div>
);
}
export default App;
JavaScript
// Intro.js file
import gfgLogo from "../assets/gfg.png";
const Intro = () => {
return (
<div className="App">
Image at src/assets/gfg.png : <br />
<img src={gfgLogo} />
<br />
Image at public/gfgPublic.png: <br />
<img
src={
process.env.PUBLIC_URL + "/logo512.png"
}
/>
<br />
</div>
);
};
export default Intro;
XML
# .env file
PUBLIC_URL = "./public/"
Output

Console window shows the external import of the png.

Conclusion
We can use the .env file to use the files from public directory. Store the files url as env variables and use in the js files as required. The alternate methods for this task invloves using the assets as static of referencing script files in public/index.html.
Similar Reads
How to Access Files Uploaded to hte Public Folder in Next.js? To access files uploaded to the public folder in Next.js, a popular React framework for building server-side rendered applications. When working with Next.js, it's important to understand how to manage files that are publicly accessible to your application users. What is a Public Folder in Next.js?T
3 min read
How to use Firestore Database in ReactJS ? Firebase is a comprehensive backend solution offered by Google that simplifies the process of building, managing, and growing applications. When developing mobile or web apps, handling the database, hosting, and authentication can be challenging tasks. Firebase addresses these challenges by providin
9 min read
How to use CDN Links in React JS? React is integrated in the projects using package managers like npm or yarn. However, there are some cases where using Content Delivery Network (CDN) links can be a advantage, especially for quick prototypes, or when integrating React into existing projects. The two libraries imported CDN Links are
3 min read
How to modularize code in ReactJS ? Modularize code in React JS refers to dividing it into segments or modules, where each file is responsible for a feature or specific functionality. React code can easily be modularized by using the component structure. The approach is to define each component into different files. With each componen
3 min read
How to publish a ReactJS component to NPM ? Follow these simple steps in order to publish your own ReactJS component to NPM. Step 1: Initial Setup In order to publish any ReactJS Component to npm (node package manager), first we have to create a React component in the React app. Following are the instructions for creating any react app. Creat
3 min read