0

How can i add a simple, classic js script to the react project? I've tried to add it just by adding

<script src="../src/alert.js"></script>

in the head of the index.html but it seems not to work!

2
  • 2
    Did you try to add it inside the index.js -> import './alert.js'; Commented Aug 2, 2020 at 22:57
  • Thx, that helped, but why cant i add it from the main index.html file? Commented Aug 2, 2020 at 23:07

1 Answer 1

1

Here the Dan Abramov's answer say

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

The index.html is use as a template and the index.js is your entry point of you app

  <body>
    <noscript>You need to enable JavaScript to run this app.</noscript>
    <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` or `yarn start`.
      To create a production bundle, use `npm run build` or `yarn build`.
    -->
  </body>

So if you want to load additional script you have to import them inside the index.js.

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

2 Comments

Ok, thx. I understand. Don't be angry but I'll ask - again why doesn't it work when i'm adding the script using classic js <script> tag?
Can You be so nice and answer my last question?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.