-2

I'm trying to add a custom javascript file to my Gatsby site but I'm still very new to Gatsby and not sure what the best way is to implement it and how. I have tried different methods but I keep getting an error:

Uncaught ReferenceError: jQuery is not defined

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
Wacke
  • 21
  • 1
  • 1
  • 3
  • 4
    Why are you trying to include jQuery in the first place? It's helpful for two things, Ajax (and Axios is a more modern library for that) and DOM manipulation (which doesn't play nicely with React which Gatsby is built on). – Quentin Aug 22 '19 at 15:03

1 Answers1

0

First of all, Gatsby is a static site generator of React and usually we won't implement JQuery with React. If you would like to manipulate your DOM by JQuery, I think you probably could do the same by React.

Back to your question, there are several ways commonly used to include a JS file in Gatsby:

The first one is include your script in Gatsby's html.js. You can refer the documentation there: https://www.gatsbyjs.org/docs/custom-html/#adding-custom-javascript

The second one is using React Helmet. It will let to embed head tags in your components. https://www.npmjs.com/package/react-helmet

Ivanhoe Cheung
  • 80
  • 2
  • 10