Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Joshua Bowden
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Joshua Bowden
Full Stack JavaScript Techdegree Graduate 29,312 Points

Setting up jQuery in Node Express Apps

I am trying to set up jQuery in my Node Expres app but the functions are not working. I have tested the JS file that I am going to work in so the link is right. Any ideas?

script(src='https://code.jquery.com/jquery-3.2.1.slim.min.js', integrity='sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN', crossorigin='anonymous')
script(src='/javascripts/script.js', type='text/javascript')

1 Answer

Natalie Cluer
Natalie Cluer
18,898 Points

Hi Josh! Jquery will not work in node.js because node.js is run in a different environment than front-end JS. Meaning, client-side js is run in the browser, giving it access to host objects like "window", and "document" (things that jquery uses). Node.js is run on the server-side, meaning it does not have access to objects provided by the browser, but has access to different host objects(i.e/ the filesystem.) The file where you are writing your back-end js using express is different from the JS that will be interacting with the DOM. You can write your client-side js separately from your express/node code, and serve it as a static file. Your client-side js is a "static asset", which can be sent directly to the browser without manipulation by the express file.

There are some npm packages like cheerio that allow a type of jquery-like manipulation but I dont think that is what you're looking for here. Hope this helps!