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

WS cannot be built for browser usage with webpack 5+ #2117

Closed
1 task done
flashd2n opened this issue Feb 13, 2023 · 1 comment
Closed
1 task done

WS cannot be built for browser usage with webpack 5+ #2117

flashd2n opened this issue Feb 13, 2023 · 1 comment

Comments

@flashd2n
Copy link
Contributor

Is there an existing issue for this?

  • I've searched for any related issues and avoided creating a duplicate issue.

Description

Hello, I maintain a library which can be used either in the browser or in Node apps. Our code has a simple environment detection and we conditionally require 'ws'. This used to work great prior to WS 8, because the "browser" field in the package.json made sure that when this lib is being bundled for the browser, the ws lib will effectively return a noop.

example snippet for ws@7.5.9 package.json:

...
  "license": "MIT",
  "main": "index.js",
  "browser": "browser.js",
  "engines": {
    "node": ">=8.3.0"
  },
...

We upgraded to ws@8.12.0 and clients started reporting issues building their React apps with our lib. We traced the issue down to the ws lib, which did not fallback to a noop. The reason for this is the included "exports" property:

example snippet for ws@8.12.0 package.json:

...
  "license": "MIT",
  "main": "index.js",
  "exports": {
    ".": {
      "import": "./wrapper.mjs",
      "require": "./index.js"
    },
    "./package.json": "./package.json"
  },
  "browser": "browser.js",
  "engines": {
    "node": ">=10.0.0"
  },
...

Exports has priority over "main" and over "browser", which effectively means that the "browser" field is ignored, resulting in errors like "Module not found: Error: Can't resolve 'stream' in '/Users/flash/Coding/temp/react-empty/node_modules/ws/lib'"

A simple solution is to add "browser" to the "exports" field like this:

...
  "license": "MIT",
  "main": "index.js",
  "exports": {
    ".": {
      "browser": {
        "default": "./browser.js"
      },
      "import": "./wrapper.mjs",
      "require": "./index.js"
    },
    "./package.json": "./package.json"
  },
  "browser": "browser.js",
  "engines": {
    "node": ">=10.0.0"
  },
...

This way bundling ws for the browser will continue to work as pre 8.

ws version

8.12.0

Node.js Version

18.13.0

System

System:
OS: macOS 13.2
CPU: (8) arm64 Apple M1 Pro
Memory: 775.80 MB / 16.00 GB
Shell: 5.8.1 - /bin/zsh

Expected result

I expect that ws would not break the webpack bundling for the browser.

Actual result

Attempting to bundle ws with webpack results in error:

Module not found: Error: Can't resolve 'stream' in '/Users/flash/Coding/temp/react-empty/node_modules/ws/lib'
Did you mean './stream'?

Attachments

No response

@flashd2n
Copy link
Contributor Author

The proposed fix is #2118

@lpinca lpinca closed this as completed in 0d114ef Feb 13, 2023
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

1 participant