How can I add jquery with a node project after installing using npm?

I installed jquery with npm (npm install jquery --save) and it added jquery to my node_modules folder. Which path am I using for the script tag in the html page?

This is how my directory structure looks like (including only what is needed)

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

      

For socket.io it looks like

<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 would be greatly appreciated! Thank!

+3


source to share


1 answer


You don't need to install the jQuery node module to run jQuery on the client side. You can simply download it from a public or static directory. For example,

The structure of your project

-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, as is recommended in the Socket.IO startup 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 files from that folder.

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

      

Express serving static:

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

+2


source







All Articles