Import socket.io-client not working

hi,
I installed npm install -g socket.io

but i can not to import
import * as io from "socket.io-client";

I was able to import
import * as io from "../../../node_modules/socket.io-client";
But when I try to run the application it throws an error:

Error TS2307: Cannot find module ‘…/…/…/node_modules/socket.io-client’.

How can I fix this?

thanks
sagi

Use:

npm install socket.io-client --save

to install things globally is not recommended :wink:

And also, install its typings with:

typings install dt~socket.io-client --save --global

so you don’t get compiler errors.

2 Likes

after runnig this command:
typings install dt~socket.io-client --save --global
I got this message:

socket.io-client
└── (No dependencies)

The error has not gone away:
Error TS2307: Cannot find module '../../../node_modules/socket.io-client'.

That error should be gone after:

npm install socket.io-client --save

without -g or --global.

Still Getting error
Error TS2307: Cannot find module ‘socket.io-client’.

Hi,
I am having the same issue after doing all the steps listed in you solution.

Error:

ngc error: Error: Error at /home/hudo/Projetos/woomieIonic/.tmp/providers/socketio-provider.ts:5:21: Cannot find module 'socket.io-client'.
Error at /home/hudo/Projetos/woomieIonic/.tmp/providers/socketio-provider.ts:16:11: Cannot find namespace 'SocketIOClient'.
Error at /home/hudo/Projetos/woomieIonic/.tmp/providers/socketio-provider.ts:16:11: Public property 'socket' of exported class has or is using private name 'SocketIOClient'.

Commands output:

> npm install --save socket.io-client
ionic-hello-world@ /home/hudo/Projetos/woomieIonic
└── socket.io-client@1.4.8 

npm WARN optional Skipping failed optional dependency /chokidar/fsevents:
npm WARN notsup Not compatible with your operating system or architecture: fsevents@1.0.14
> typings install dt~socket.io-client --save --global
socket.io-client
└── (No dependencies)
> typings search socket.io-client
Viewing 1 of 1

NAME             SOURCE HOMEPAGE          DESCRIPTION VERSIONS UPDATED                 
socket.io-client dt     http://socket.io/             1        2016-03-17T12:06:54.000Z
\\package.json
\\...
"dependencies": {
    "@ionic/storage": "^1.0.3",
    "ionic-angular": "^2.0.0-rc.0",
    "ionic-native": "^2.0.3",
    "ionicons": "^3.0.0",
    "socket.io-client": "^1.4.8"
  },
\\...

Do you have any clue of what can be happening?

Thanks,
Hudo.

I’m also having the same issue. Could you solve it @matheo?

Edit: I solved it by adding /// <reference path="../../typings/globals/socket.io-client/index.d.ts" /> at the top of my app.components.ts

2 Likes

Thank you very much Aristona! Adding the reference at the top of my component file works well.

Curiosity, is there are any alternative without the reference path solution?

Thank you again!

@sderrico Nope. Could you find a solution yourself?

try installing:

npm i @types/socket.io-client

^ This worked for me :+1:

1 Like