Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: io is not a function #1287

Closed
1 of 2 tasks
weeping-somnambulist opened this issue Apr 10, 2019 · 13 comments
Closed
1 of 2 tasks

TypeError: io is not a function #1287

weeping-somnambulist opened this issue Apr 10, 2019 · 13 comments

Comments

@weeping-somnambulist
Copy link

weeping-somnambulist commented Apr 10, 2019

You want to:

  • report a bug
  • request a feature

Current behaviour

socket.io does not work, throws obtuse error message when trying to implement. Issue is most likely in documentation, code quality (code comments are helpful here, these are useless), poor answers in issue tracker.

Basically a duplicate of #873, but that was never really answered to we get to do this all over again.

Steps to reproduce (if the current behaviour is a bug)

My code stub (I've tried all kinds of permutations of this):

const io = require('socket.io.slim.js');

var socket = io('http://localhost:9990'); // TIP: io() with no args does auto-discovery
socket.on('connect', function () {
socket.emit('ferret', 'tobi', 'woot', function (data) { console.log(data); // data will be 'tobi says woot' });
});

// error message:

2019-04-09 13:08:46.166650 [ERR] undefined:6 Exception: TypeError: io is not a function (near: "var socket = io('http://localhost:9990'); // TIP: io() with no args does auto-discovery")

Expected behaviour

It to work as shown in the example.

Setup

  • OS: debian 9
  • browser: chrome v8 engine
  • socket.io version: commit 9c1e73c752aec63f48b511330a506d037783d897

Other information (e.g. stacktraces, related issues, suggestions how to fix)

See issue #873 for a whole bunch of other people all asking the same question.

@kapi2289
Copy link

If you downloaded this as a file, you need to add ./ before the name and you don't need to add .js extension

const io = require('./socket.io.slim');

@weeping-somnambulist
Copy link
Author

Tried that, same behavior.

@kapi2289
Copy link

Try using this .js file in a <script> tag

<script src="/socket.io.slim.js"></script>
<script>
	var socket = io('http://localhost:9990');
</script>

OR

require('./socket.io.slim');
var socket = io('http://localhost:9990');

@weeping-somnambulist
Copy link
Author

I am not launching this script via HTML. I've tried that exact block of code listed and got the exact same error message I gave to start with.

@kapi2289
Copy link

If you're using npm it would be better to install this lib using it

$ npm i socket.io-client

and then

const io = require("socket.io-client");

@weeping-somnambulist
Copy link
Author

I am not using NPM. I am using the Chrome V8 engine - straight.

@kapi2289
Copy link

kapi2289 commented Apr 10, 2019

Is this for sure a client, not a server script? I've tried to reproduce your code, I've downloaded socket.io.slim.js from https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.2.0/socket.io.slim.js and the only error I got was missing document ('cause I didn't run this in a browser)

> const io = require("./socket.io.slim");
> io("http://localhost:8888");
ReferenceError: document is not defined

@weeping-somnambulist
Copy link
Author

It's a client, as in it is connecting to a socket as a client.

@davidwitten
Copy link

I'm running into this same issue. Were you able to find a solution @SwitchedToGitlab?

@darrachequesne
Copy link
Member

For future readers, please see the documentation here: https://socket.io/docs/v3/client-initialization/

@adiswordfish
Copy link

Has anybody found the solution?

@mmirann
Copy link

mmirann commented Feb 10, 2022

This causes an error TypeError: io is not a function

const io = require("socket.io-client");

Solve it by using it like this:

const { io } = require("socket.io-client");

or

const io = require("socket.io-client").io;

@aliparsa
Copy link

same here by using npm -> TypeError: io is not a function

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants