canirequire › Dual (CommonJS + ESM) › ws
Is ws ESM-only? Can you require() it?
Simple to use, blazing fast and thoroughly tested websocket client and server for Node.js
Last verified Sep 9, 2026 against ws@8.21.3. Markdown · JSON
Dual (CommonJS + ESM) Dual package — require() works on every Node.js version we test; import works too
ws publishes both a CommonJS entry and a native ESM entry (ESM added in 8.0.0). require("ws") works on every Node.js version we test (18, 20, 22, 24 and 26) and in Bun. The package declares Node.js >=10.0.0. import "ws" works alongside it.
What to do
- Nothing to change. require("ws") works on every Node.js version we test (18, 20, 22, 24 and 26) and in Bun. The package declares Node.js >=10.0.0. import * as ws from "ws" works alongside it; use named imports for what the ESM entry exports.
- TypeScript: npm install -D @types/ws
Does ws@8.21.3 actually load? Runtime test results
Installed and executed on Sep 9, 2026 with require("ws") from a CommonJS file and import "ws" from an ES module.
| Runtime | require("ws") | import "ws" |
|---|---|---|
| Node.js 18 | ✓ works | ✓ works |
| Node.js 20 | ✓ works | ✓ works |
| Node.js 22 | ✓ works | ✓ works |
| Node.js 24 | ✓ works | ✓ works |
| Node.js 26 | ✓ works | ✓ works |
| Bun 1.4.0 | ✓ works | ✓ works |
How to import ws
ES modules (import)
import * as ws from "ws";Namespace import shown because the ESM entry may have no default export; use named imports for what it exports.
CommonJS (require)
const ws = require("ws");Module format by version
Derived from the type, main and exports fields of every stable release on npm, corrected where a runtime test of that version contradicted them (181 versions; the newest 60 shown).
| Versions | Format | First published |
|---|---|---|
| 8.0.0 – 8.21.3 | Dual (CommonJS + ESM) | Jul 28, 2021 |
| 7.4.1 – 7.5.13 | CommonJS | Dec 4, 2020 |
Frequently asked
Is ws ESM-only?
ws@8.21.3 ships CommonJS and ESM builds. ws publishes both a CommonJS entry and a native ESM entry (ESM added in 8.0.0). require("ws") works on every Node.js version we test (18, 20, 22, 24 and 26) and in Bun. The package declares Node.js >=10.0.0. import "ws" works alongside it.
Can I require ws?
Yes. ws ships a CommonJS entry alongside the native ESM entry. require("ws") works on every Node.js version we test (18, 20, 22, 24 and 26) and in Bun. The package declares Node.js >=10.0.0.
Does ws work in Bun?
Yes. import "ws" loaded successfully in Bun 1.4.0 on Sep 9, 2026.
Does ws ship TypeScript types?
Not bundled. Install the community types with "npm install -D @types/ws".
Links
Other dual (commonjs + esm) packages
- minimatch 10.2.6Dual
- brace-expansion 5.0.9Dual
- lru-cache 11.5.2Dual
- tslib 2.8.1Dual
- glob 13.0.6Dual
- eslint-visitor-keys 5.0.1Dual
- yallist 5.0.0Dual
- balanced-match 4.0.4Dual
Similar popularity
- path-key 4.0.0ESM-only
- typescript 7.0.2CommonJS
- isexe 4.0.0Dual
- esbuild 0.28.2CommonJS
- is-fullwidth-code-point 5.1.0ESM-only
- argparse 3.0.1CommonJS
- globals 17.12.0CommonJS
- yargs 18.1.0ESM-only