0

I need add my local javascript file to my gatsby project. The file is a library. What I tried to do:

1) put myscript.js to ./src and import it in ./src/html.js file as import from './myscript.js';

2) put myscript.js to ./src and import it in ./src/html.js file as <script type='text/javascript' src="./myscript.js"/>

3) put myscript.js to ./public and import it in both ways

But none of this works. Please, tell me how to do it?

Farad
  • 885
  • 2
  • 9
  • 25
  • Does this answer your question? [Adding script tag to React/JSX](https://stackoverflow.com/questions/34424845/adding-script-tag-to-react-jsx) – Ali Mousavi Jan 15 '20 at 00:16

1 Answers1

2

What is it that you are trying to add? What kind of library? You shouldn't be adding scripts to the Html file (though it is possible) If you want to use an external lib then you install it via npm or yarn and import it in your components.

Check here for adding a custom script to the Html file in a gatsby project: add script to Html in gatsby

You can also check here which details using different methods for inserting scripts: more ways to do it

Edit:

You can try using the static folder. Check this answer here:use static folder gatsby

molebox
  • 585
  • 1
  • 8
  • 17
  • it is prebid.js file. It is library for header bidding. It doesn't have any npm modules and every programmer generates his own file – Farad Jan 15 '20 at 09:48
  • @Farad so you want to add a script to the site header? You can use React Helmet for that. Or check the links I added to my answer – molebox Jan 15 '20 at 09:53
  • thanks, but both of these ways uses src="https://....js", so their js file located somewhere in the Internet. Am I right? But I have this file localy – Farad Jan 15 '20 at 10:09
  • one more thing that i tried - to add ` – Farad Jan 15 '20 at 10:12
  • @Farad ok. You can try using the static folder. Check the link I added above – molebox Jan 15 '20 at 10:22
  • I put my file to static folder and just added `` to my html.js file. I am not sure but it seems it works, thanks – Farad Jan 15 '20 at 10:34