4

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!

4
  • 1
    You would need to configure your server to serve the .js file at that location. Normally you have a public/ folder that contains static files so that you can easily just serve the whole folder. Commented Jul 29, 2015 at 14:45
  • 1
    You don't need a node module to use jQuery on the client side. Are you using anything like ExpressJs? And you really don't need to use jQuery on the backend side as most of what jQuery does is DOM manipulation. Commented Jul 29, 2015 at 14:58
  • I am using express. So I should just include it with a cdn? I'm new to this npm workflow. I assumed that you added everything like that. Commented Jul 29, 2015 at 15:12
  • 1
    You could use a CDN or you can create your own static directory in Express. This is where you would server your images, css, and client side JS from. Commented Jul 29, 2015 at 15:16

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for clarifying. I will change it.
Why are people down voting this post? I feel like I asked a legitimate question. I'm just trying to learn how all of this works, and I couldn't find anything that explicitly said how I should do it.
@billabrian6, Sorry I couldn't tell ya. Sent an upvote your way to remove the downvote. Sometimes people are impatient and don't wait for the question to get hashed out a little better. Just my guess..
Just not what I expect from this community. I've always had good experiences asking questions here, and I always try to include as much info as possible to enable someone to help me. Again, thanks for helping out!
Sure no problem. Glad to help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.