Dirk
Dirk

Reputation: 3231

Script listed in Jade not accessible in my JavaScript

In running an express app, the Jade template includes two script lines:

  script(src="https://cdn.socket.io/socket.io-1.3.5.js")  <== library
  script(src="main.js")                                   <== my code

But when I call something defined in the first script from my JavaScript in main.js, I get the error:

 Uncaught TypeError: Cannot read property 'connect' of undefined

The offending line is this one:

 var io = io.connect();

Which is defined in the first socket.io script. How can I include that script in a way that my code in main,js can find it?

If this was server side, I'd just 'require' it.

Upvotes: 0

Views: 66

Answers (2)

Craicerjack
Craicerjack

Reputation: 6332

Regarding accessing the io javascript
You can check in your developer tools in network or console tab to find if the socket js is loading in your templates.

you can install it with node:

npm install socket.io    
var io = require('socket.io')(80);

Other than that you can go directly to this page https://cdn.socket.io/socket.io-1.3.5.js and copy the code from that page into a local js file (socket.js or whatever you'd like to call it) and then require it as you would any other file.

Or find a different cdn to try and access it from.

Regarding the error you are getting
Have you defined io anywhere?

Upvotes: 1

Luca Colonnello
Luca Colonnello

Reputation: 1456

Try this version

https://cdnjs.cloudflare.com/ajax/libs/socket.io/1.3.5/socket.io.js

https://cdnjs.cloudflare.com/ajax/libs/socket.io/1.3.5/socket.io.min.js

Socket io use browserify in order to compile the script into one file.

Probably the version you use is corrupt. If this doesn't solve, try to use the npm socket-io.client lib.

npm install --save socket.io

<script src="/socket.io/socket.io.js"></script>
<script>
  var socket = io();
</script>

Translate this into jade.

Upvotes: 1

Related Questions