Cannot read properties of null (reading ‘attachShadow’)

I’m rendering a react app(Widget) inside Shadow DOM. So I looked at the div id where the widget is getting loaded currently.

See screenshow below. enter image description here

With this information, I want to create Shadow host like this.


const host = document.querySelector('#widget-_hw');
const shadow = host.attachShadow({ mode: 'open' });
const renderIn = document.createElement('div');

But I’m getting this error.

Cannot read properties of null (reading 'attachShadow')