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.

MessageChannel is not defined

See original GitHub issue

Basic info:

I am attempting to use Zombie.js (which uses jsdom) to debug Message Channel generation, but it doesn’t seem to be defined. According to the MDN docs, the MessageChannel constructor has support going all the way back to IE10, so it seems strange that it’s not supported here

  • Node.js version: 10.10.0
  • jsdom version: 13.0.0

Minimal reproduction case

'use strict';

// Let's see if MessageChannel is supported
let html = `
<!DOCTYPE html>
<body>
  <h1 id='title'>
    Does MessageChannel Exist?
  </h1>
  <script>
    var title = document.getElementById('title');
    try {
      window.channel = new MessageChannel();
    } catch(e) {
      console.log('Error caught: ' + e.message)
    }

    if (window.channel) {
    	title.innerHTML = 'MessageChannel Exists!';
    } else {
    	title.innerHTML = 'MessageChannel Does not Exist :(';
    }
  </script>
</body>
</html>
`;

const { JSDOM } = require('jsdom');
const { window } = new JSDOM(html, {runScripts: 'dangerously'});

console.log(window.eval('document.getElementById("title").innerHTML'));
// Expected -> 'MessageChannel Exists!'
// Actual ---> 'MessageChannel Does not Exist :('

Running the above node file should print the string “MessageChannel Exists!” and produce no errors. However, when running using jsdom a “MesssageChannel is not defined” error is thrown, and the console logs “MessageChannel Does not Exist 😦”

How does similar code behave in browsers?

Example of working code in JSFiddle: https://jsfiddle.net/hvLpg6tm/5/

The HTML provided is exactly the same

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:7
  • Comments:6

github_iconTop GitHub Comments

14reactions
brettz9commented, Sep 29, 2019

Looks like one could just do this as of Node 10.5.0:

window.MessageChannel = require('worker_threads').MessageChannel;

Source: https://nodejs.org/api/worker_threads.html#worker_threads_class_messagechannel

However, one needs to pass Node the --experimental-worker flag.

3reactions
gaearoncommented, Mar 18, 2021

If someone adds this, there is one concern that I think needs to be resolved.

jsdom sandboxes timers. I.e. setTimeout and setInterval don’t hold the Node process hostage if you call window.close(). They’re automatically cancelled.

I believe that jsdom should do the same for MessageChannel ports, if it were to add window.MessageChannel. Concretely, window.close() should also close all open ports. Otherwise, the Node process won’t exit.

Read more comments on GitHub >

github_iconTop Results From Across the Web

ReferenceError: MessageChannel is not defined #2984 - GitHub
Basic info: Node.js version: v12.16.1 jsdom version: 16.2.2 Minimal reproduction case const axios = require("axios"); const { JSDOM } ...
Read more >
'MessageChannel is undefined' triggered on pages with ...
I'm currently trying to scrape some sites that have recaptcha implemented, but whenever i load the page's source i get this error: (node:15536) ......
Read more >
MessageChannel - Web APIs | MDN
The MessageChannel interface of the Channel Messaging API allows us to create a new message channel and send data through it via its...
Read more >
lightning web components - MessageChannel not working
Message Channel definition : <?xml version="1.0" encoding="UTF-8"?> <LightningMessageChannel xmlns="http://soap.sforce.com/2006/04/metadata"> ...
Read more >
MessageChannel JavaScript and Node.js code examples
setImmediate}catch(c){K=function(a){var b=new MessageChannel;b.port1.onmessage=a;b.port2.postMessage(void 0)}}return K(a)} undefined.
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