Search code examples
javascriptreactjscreate-react-app

How to load a production create-react-app script inside an html page?


I've created a create-react-app simple application (as a lib) and want to load it inside an HTML page as an external script.

  1. I created a create-react-app with the command npx create-react-app my-lib

  2. Changed my-lib/src/index.js to this:

enter image description here

  1. Inside the my-lib directory I run yarn build. This generated the following production files:

enter image description here

  1. Created an external HTML file loading the production version of my-lib.

enter image description here

I'm getting the following error:

enter image description here


Solution

  • Production build creates 2 JS files i.e. chunk.js and main.js so we need to import both in index.html file

    1. Loaded react component in variable and assigned to window

    enter image description here

    1. Called same window variable in index.html

    enter image description here

    Please try this way