How to import socket.io-client in a angular 2 application?

In order to register the module so you can import it, you need to include it in you SystemJS configuration.

System.config({
    packages: {
        ...
        "socket.io-client": {"defaultExtension": "js"}
    },
    map: {
        "socket.io-client": "https://stackoverflow.com/questions/37090031/node_modules/socket.io-client/socket.io.js"
    }
});

And change your import to:

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

Also, you don’t need the import in the script tag anymore, so remove:

<script src="https://stackoverflow.com/questions/37090031/node_modules/socket.io-client/socket.io.js"></script>

Finally, if you like to add the typings, add in your typings.json:

{
  "ambientDependencies": {
    ...
    "socket-io-client":"github:DefinitelyTyped/DefinitelyTyped/socket.io-client/socket.io-client.d.ts#7de6c3dd94feaeb21f20054b9f30d5dabc5efabd"
  }
}

P.S. Int the future, change the hash in the typings to the latest commit hash.

Leave a Comment