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

inconsistent typing and actual export structure would cause runtime error using with webpack and typescript [v3.0.0] #1396

Closed
1 of 2 tasks
noe132 opened this issue Nov 9, 2020 · 6 comments
Labels
bug Something isn't working
Milestone

Comments

@noe132
Copy link

noe132 commented Nov 9, 2020

You want to:

  • report a bug
  • request a feature

Current behaviour

When using with webpack and typescript,

import { io } from 'socket.io-client';

is valid for typescript, but would result an error in runtime.
because when typescript looks for typing, it gets build/index.d.ts,
and webpack try resolve path for the package, and it looks package.json, and found there is field ["exports"]["."]["import"] and use its value ./wrapper.mjs, but has an different export structure than the typing above.

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

Setup a minimal webpack ts-loader build, and using io would get this error message

Expected behaviour

warpper.mjs should have same export structure as build/index.js

Setup

  • OS: win10
  • browser: chrome 86
  • socket.io version: 3.0.0
@Tallyrald
Copy link

Tallyrald commented Nov 9, 2020

My problem is probably caused by this so I'll just comment here instead of opening another ticket:
Before v3.0.0 I could do this:

import SocketIO from 'socket.io-client';
let socket = SocketIO.connect('some URL string', { transports: ['websocket'] });

Now I can't. v3.0.1 exposes only 4 properties: protocol, Manager, io, Socket. No methods or subproperties in any of them. Am I missing something? Did the usage pattern change, but it's missing from the changelog?
I'm using Typescript v4.0.5

Edit: Looking at the SocketIO docs I found that the SocketIO export should have a constructor that behaves like the previous connect method, but this is entirely missing from the typings.

@darrachequesne
Copy link
Member

@Tallyrald you should be able to use:

import { io }  from 'socket.io-client';

let socket = io('some URL string', { transports: ['websocket'] });

I think we should have removed the connect() method in v3, as it's just an alias.

@noe132 I could indeed reproduce the issue, as CommonJS and ES module definitions are out of sync.

@darrachequesne darrachequesne added the bug Something isn't working label Nov 9, 2020
@Tallyrald
Copy link

Oh, you're completely correct, it seems my train of thoughts derailed somewhere between the version upgrade and the seemingly missing exports. Everything is fine, thank you!

@ShaneMcGowan
Copy link

Having the same issue. Was starting a new project using socket.io and socket.io-client.

Set Up

OS: win10
browser: chrome 86
socket.io version: 3.0.0

Issue

Webpack is complaining and saying
export 'io' (imported as 'io') was not found in 'socket.io-client'

Work Around

Downgrading to V2.3.1 allowed webpack to bundle everything together and run without issue

darrachequesne added a commit that referenced this issue Nov 17, 2020
The TypeScript (build/index.d.ts) and ES modules exports did not match,
which caused issues with webpack and ts-loader.

Related: #1396
darrachequesne added a commit that referenced this issue Nov 17, 2020
The TypeScript (build/index.d.ts) and ES modules exports did not match,
which caused issues with webpack and ts-loader.

Related: #1396
@mgroenhoff
Copy link

I'm now getting this error when doing import { io } from "socket.io-client"; in my code which is bundled using webpack:

ERROR in ./node_modules/socket.io-client/wrapper.mjs 4:13
Module parse failed: Identifier 'io' has already been declared (4:13)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
|
| export const Manager = io.Manager;
> export const io = io;
| export default io;
|

@darrachequesne
Copy link
Member

This should be fixed in socket.io-client@3.0.3.

Closed by bec1524.

@darrachequesne darrachequesne added this to the 3.0.3 milestone Nov 20, 2020
sunrise30 added a commit to sunrise30/socket.io-client that referenced this issue Jan 8, 2022
The TypeScript (build/index.d.ts) and ES modules exports did not match,
which caused issues with webpack and ts-loader.

Related: socketio/socket.io-client#1396
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

5 participants