Jils
Jils

Reputation: 783

Angular 2: error TS2307: Cannot find module 'socket.io-client'

After having installed the module socket.io

npm install socket.io --save

I have the following error:

error TS2307: Cannot find module 'socket.io-client'

import

import * as io from 'socket.io-client';

systemjs.config.js

var map = {
    'socket.io-client': 'node_modules/socket.io-client/socket.io.js'
}

var packages = {
    'socket.io-client': { main: 'socket.io', format: 'cjs', defaultExtension: 'js' }
}

package.json

"dependencies": {
    "socket.io": "^1.4.8"
}

typings.d.ts

/// <reference path="../socket.io-client/socket.io.js" />

declare module 'socket.io-client' {
  var e: any;
  export = e;
}

socket.io-client (Directory)
- socket.io.js
- typings.d.ts


Angular 2 RC5

Upvotes: 12

Views: 24178

Answers (3)

j2L4e
j2L4e

Reputation: 7060

Update 2021

@nullromo pointed out, the socket.io-client package now has typings built-in. Installing the types separately is no longer necessary

Update 2018

To properly use socket.io in the browser you need to install both the socket.io client package and its typings:

npm i socket.io-client @types/socket.io-client 

outdated:

You are missing typings. Open typings.d.ts and add

declare module 'socket.io-client' {
  var e: any;
  export = e;
}

You can also try to install typings for socket.io via npm i @types/socket.io-client. I don't know if there are typings available, though.

Upvotes: 28

Lucas
Lucas

Reputation: 10313

Make sure to install the correct package with type definitions for socket.io:

npm install @types/socket.io-client --save

This will include the types in the correct folder and means you would need no further action in any other file since angular will pick this up.

Upvotes: 16

enRaiser
enRaiser

Reputation: 2646

I solved it by adding this at the top of my app.components.ts

  /// <reference path="../../typings/globals/socket.io-client/index.d.ts" /> 

Upvotes: 0

Related Questions