Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add jquery with node project after installing with npm?

I have installed jquery with npm (npm install jquery --save) and it added jquery to my node_modules folder. What path do I use for the script tag on the html page?

Here is what my directory structure looks like (only included what is necessary)

-Project Root
  -node_modules
      -jquery
          -dist
              -jquery.js
          -src
  -index.js
  -index.html
  -package.json

For socket.io it looks like this

<script src="/socket.io/socket.io.js"></script>

I tried this for jquery but it didn't work

<script src="/jquery/dist/jquery.js"></script>

Any help will be greatly appreciated! Thanks!

like image 989
billabrian6 Avatar asked Sep 01 '25 17:09

billabrian6


1 Answers

You don't need to install a jQuery node module to run jQuery on the client side. You should be able to just load it from a public or static directory. For example,

Your project Structure

-Project Root
  -public
     -js
       jquery.js
  -node_modules
  -index.js
  -index.html
  -package.json

HTML

<!doctype html>
<html>
    <head>
        <script src="/socket.io/socket.io.js"></script>
        <script src="public/js/jquery.js"></script>
    </head>
    <body>
        ... stuff ...
    </body>
</html>

app.js

I am assuming you are using Express, like what is recommended in the Socket.IO getting started example. http://socket.io/get-started/chat/ So in your app.js you need to declare your public or static location. This will allow your index.html to access the files from within that folder.

app.use('/public', express.static('public'));

Express serving static:

http://expressjs.com/starter/static-files.html

like image 181
Kris Hollenbeck Avatar answered Sep 04 '25 11:09

Kris Hollenbeck