question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot find name 'MessageEvent'

See original GitHub issue

I changed the value for lib in my “tsconfig.json” from ["dom", "es2017"] to ["es2017"] because I am working on a Node.js-only project (no Browser environment). Unfortunately, this broke my integration with “reconnecting-websocket”. I am getting now the following errors:

##[error]node_modules/reconnecting-websocket/dist/events.d.ts(20,14): error TS2304: Cannot find name 'MessageEvent'.
##[error]node_modules/reconnecting-websocket/dist/events.d.ts(30,22): error TS2304: Cannot find name 'MessageEvent'.
##[error]node_modules/reconnecting-websocket/dist/events.d.ts(31,30): error TS2304: Cannot find name 'MessageEvent'.
##[error]node_modules/reconnecting-websocket/dist/reconnecting-websocket.d.ts(24,54): error TS2304: Cannot find name 'Blob'.
##[error]node_modules/reconnecting-websocket/dist/reconnecting-websocket.d.ts(54,17): error TS2304: Cannot find name 'BinaryType'.
##[error]node_modules/reconnecting-websocket/dist/reconnecting-websocket.d.ts(96,25): error TS2304: Cannot find name 'MessageEvent'.

When digging into “reconnecting-websocket.ts”, I realized that it expects “MessageEvent” to come from TypeScript’s internal “DOM” library which is not available in a Node.js environment.

On Node.js the MessageEvent can be retrieved from the “ws” package:

import WebSocket, {MessageEvent} from 'ws';

Can this information be included in “reconnecting-websocket”?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
HouCodercommented, Jul 7, 2020

Thanks @bennyn for your quick response.

0reactions
bennycodecommented, Jun 10, 2021

Ideally, “reconnecting-websocket” would use “@types/ws” which has a definition for WebSocket.MessageEvent:

https://github.com/DefinitelyTyped/DefinitelyTyped/blob/47e9cf6d8947e46a82bb09bbcc3dd74a3ac0ffa1/types/ws/index.d.ts#L188-L192

Unfortunately, “reconnecting-websocket” currently relies on the “dom” lib that ships with TypeScript. That’s why you would have to write your own definition for MessageEvent if you like to use it with React Native.

Example:

declare global {
  interface MessageEvent {
    data: string;
    type: string;
  }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

TypeScript complains about DOM types when compiling for ...
d.ts(10,29): error TS2304: Cannot find name 'MessageEvent'. node_modules/rxjs/observable/dom/WebSocketSubject.d.ts( ...
Read more >
node.js - RxJS5 TypeScript typings fail - Stack Overflow
ts(36,23): error TS2304: Cannot find name 'MessageEvent'. does anyone know why this is happening? this is my package.json file: { "name": " ...
Read more >
MessageEvent - Web APIs - MDN Web Docs - Mozilla
The MessageEvent interface represents a message received by a target object. This is used to represent messages in:.
Read more >
message event - Slack API
A message is delivered from several sources: They are sent via the Real Time Messaging API when a message is sent to a...
Read more >
Activiti User Guide
The source code of Activiti can be found on https://github.com/Activiti/ ... name are internal implementation classes and cannot be considered stable.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found