144

I'm starting to play with the Create React App, but I can't understand how the index.js is loaded inside index.html. This is the html code:

<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico">
    <!--
      Notice the use of %PUBLIC_URL% in the tag above.
      It will be replaced with the URL of the `public` folder during the build.
      Only files inside the `public` folder can be referenced from the HTML.

      Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
      work correctly both with client-side routing and a non-root public URL.
      Learn how to configure a non-root public URL by running `npm run build`.
    -->
    <title>React App</title>
  </head>
  <body>
    <div id="root"></div>
    <!--
      This HTML file is a template.
      If you open it directly in the browser, you will see an empty page.

      You can add webfonts, meta tags, or analytics to this file.
      The build step will place the bundled scripts into the <body> tag.

      To begin the development, run `npm start`.
      To create a production bundle, use `npm run build`.
    -->
  </body>
</html>

But I can't see anywhere the import of the index.js. Where is the connection? What am I missing?

6
  • How do you serve your content? Feb 24, 2017 at 12:06
  • I haven't changed anything default, I just do this: create-react-app my-app and I open the project using webstorm, then I do npm start, but my question is how index.html know that have to load index.js? where is write?
    – Piero
    Feb 24, 2017 at 12:08
  • 7
    webpack.github.io/docs/webpack-dev-server.html Webpack-dev-server handles this for you in development. Feb 24, 2017 at 12:09
  • 1
    @NicklasWinger Where I can find the configuration file to understand the mechanism?
    – Piero
    Feb 24, 2017 at 13:12
  • 4
    You can run npm run eject to manually control the configs. I recommend doing a more basic setup from scratch to fully understand what's going on :) Feb 24, 2017 at 13:24

1 Answer 1

198

Under the hood, Create React App uses Webpack with html-webpack-plugin.

Our configuration specifies that Webpack uses src/index.js as an “entry point”. So that’s the first module it reads, and it follows from it to other modules to compile them into a single bundle.

When webpack compiles the assets, it produces a single (or several if you use code splitting) bundles. It makes their final paths available to all plugins. We are using one such plugin for injecting scripts into HTML.

We have enabled html-webpack-plugin to generate the HTML file. In our configuration, we specified that it should read public/index.html as a template. We have also set inject option to true. With that option, html-webpack-plugin adds a <script> with the path provided by Webpack right into the final HTML page. This final page is the one you get in build/index.html after running npm run build, and the one that gets served from / when you run npm start.

The beauty of Create React App is you don’t actually need to think about it.

8
  • One more question, what IDE do you use for react or js in general? I have fought in the choice between Atom and Webstorm, what do you think?
    – Piero
    Feb 24, 2017 at 13:47
  • 1
    I'm using Webstorm and so far I'm really happy with it.
    – Roberto
    Jun 13, 2017 at 9:39
  • 4
    How do you resolve multiple paths to load different HTML pages? Jan 2, 2018 at 14:36
  • 29
    Actually @Dan Abramov is a genius :D but i don't agree with him when he said "you don’t actually need to think about it" because everybody should understand things from the bottom up. Apr 22, 2020 at 14:58
  • 5
    Hopefully this is documented somewhere else besides here? In CRA documentation? We don't need to know the implementation but we do need to know things start with index.js and how it mangles public/index.html Thanks to all! Jun 3, 2020 at 5:09

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.