# Working in JSDOM or Node.js natively

**URL:** <https://discuss.codemirror.net/t/working-in-jsdom-or-node-js-natively/138>\
**Category:** Uncategorized\
**Created:** [January 23, 2015, 6:47pm UTC](https://discuss.codemirror.net/t/working-in-jsdom-or-node-js-natively/138 "2015-01-23T18:47:34Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![boucher](https://discuss.codemirror.net/user_avatar/discuss.codemirror.net/boucher/32/38_2.png) [@boucher](https://discuss.codemirror.net/u/boucher)\
**Post date:** [January 23, 2015, 6:47pm UTC](https://discuss.codemirror.net/t/working-in-jsdom-or-node-js-natively/138/1 "2015-01-23T18:47:34Z")

</div>

Just curious if anyone has tried getting codemirror running inside of a virtual dom environment like JSDOM. I’m generating server side html with React and then bootstrapping codemirror in the browser, but this leads to a rather long and unpleasant flash of unstyled text area. I’d like to be able to generate a full (static) DOM tree with codemirror so that things are at least styled before they are functional. The current attempt in JSDOM leads to: TypeError: Object [BODY] has no method ‘createTextRange’, which I’m looking into.

---

<div class="post-metadata">

**Author:** ![boucher](https://discuss.codemirror.net/user_avatar/discuss.codemirror.net/boucher/32/38_2.png) [@boucher](https://discuss.codemirror.net/u/boucher)\
**Post date:** [January 23, 2015, 7:01pm UTC](https://discuss.codemirror.net/t/working-in-jsdom-or-node-js-natively/138/2 "2015-01-23T19:01:25Z")

</div>

Alright, injecting this code is enough to get it to not error out:

```
        document.createRange = function() {
            return {
                setEnd: function(){},
                setStart: function(){},
                getBoundingClientRect: function(){
                    return {right: 0};
                }
            }
        };

```

It still doesn’t render correctly, but I’ll keep working on it. I see PhantomJS is how you’re running tests, but I think it will be too heavy for my use case. I may try that out as well.

---

<div class="post-metadata">

**Author:** ![danielzm](https://discuss.codemirror.net/user_avatar/discuss.codemirror.net/danielzm/32/102_2.png) [@danielzm](https://discuss.codemirror.net/u/danielzm)\
**Post date:** [July 30, 2015, 10:18pm UTC](https://discuss.codemirror.net/t/working-in-jsdom-or-node-js-natively/138/3 "2015-07-30T22:18:38Z")

</div>

Hey man!, were you able to run it in the backend or not? I’m getting an error:

/Users/danielzamorano/workspace/basepath/node\_modules/codemirror/lib/codemirror.js:25  
var gecko = /gecko/\d/i.test(navigator.userAgent);

I’m also using JSDOM

---

<div class="post-metadata">

**Author:** ![danielzm](https://discuss.codemirror.net/user_avatar/discuss.codemirror.net/danielzm/32/102_2.png) [@danielzm](https://discuss.codemirror.net/u/danielzm)\
**Post date:** [July 30, 2015, 11:46pm UTC](https://discuss.codemirror.net/t/working-in-jsdom-or-node-js-natively/138/4 "2015-07-30T23:46:12Z")

</div>

Nevermind I found the solution:

var expect = require(‘chai’).expect;  
var jsdom = require(‘node-jsdom’);  
var sinon = require(‘sinon’);

// virtual DOM environment for codemirror  
global.navigator = ‘gecko’;  
global.document = jsdom.jsdom();  
global.window = global.document.defaultView;

var markdownEditor = require(’…/…/…/app/Authoring/Edit/markdown\_editor.js’);

---

<div class="post-metadata">

**Author:** ![KoulSlou](https://discuss.codemirror.net/letter_avatar_proxy/v4/letter/k/bb73d2/32.png) [@KoulSlou](https://discuss.codemirror.net/u/KoulSlou)\
**Post date:** [June 2, 2017, 9:25pm UTC](https://discuss.codemirror.net/t/working-in-jsdom-or-node-js-natively/138/5 "2017-06-02T21:25:32Z")

</div>

I used boucher’s approach and that’s how initialization looks for me (I believe you shoud define create **Text** Range, not just createRange):

```
    import jsdom from 'jsdom'
    const doc = jsdom.jsdom('<!doctype html><html><body></body></html>')
    global.document = doc
    global.window = doc.defaultView
    global.document.body.createTextRange = function() {
        return {
            setEnd: function(){},
            setStart: function(){},
            getBoundingClientRect: function(){
                return {right: 0};
            },
            getClientRects: function(){
                return {
                    length: 0,
                    left: 0,
                    right: 0
                }
            }
        }
    }
```

---

<div class="post-metadata">

**Author:** ![KoulSlou](https://discuss.codemirror.net/letter_avatar_proxy/v4/letter/k/bb73d2/32.png) [@KoulSlou](https://discuss.codemirror.net/u/KoulSlou)\
**Post date:** [June 2, 2017, 9:30pm UTC](https://discuss.codemirror.net/t/working-in-jsdom-or-node-js-natively/138/6 "2017-06-02T21:30:51Z")

</div>

Still see this warning `document.body.createTextRange is not a function`

---

<div class="post-metadata">

**Author:** ![colel](https://discuss.codemirror.net/user_avatar/discuss.codemirror.net/colel/32/4410_2.png) [@colel](https://discuss.codemirror.net/u/colel)\
**Post date:** [October 30, 2020, 11:33am UTC](https://discuss.codemirror.net/t/working-in-jsdom-or-node-js-natively/138/8 "2020-10-30T11:33:54Z")

</div>

I was able to implement a similar polyfill by instead putting it on the Document prototype  
Something like the following:

```
const polyfills = {
  createRange() {
    // @ts-ignore
    Document.prototype.createRange = function () {
      return {
        setEnd: function () {},
        setStart: function () {},
        getBoundingClientRect: function () {
          return { right: 0 }
        },
        getClientRects: function () {
          return {
            length: 0,
            left: 0,
            right: 0,
          }
        },
      }
    }
  },
}
polyfills.createRange()

```

This was useful for ProseMirror, but something similar may be used to fix this in CodeMirror as well.

---

<div class="post-metadata">

**Author:** ![bpstrngr](https://discuss.codemirror.net/user_avatar/discuss.codemirror.net/bpstrngr/32/4715_2.png) [@bpstrngr](https://discuss.codemirror.net/u/bpstrngr)\
**Post date:** [November 2, 2020, 5:48pm UTC](https://discuss.codemirror.net/t/working-in-jsdom-or-node-js-natively/138/9 "2020-11-02T17:48:52Z")

</div>

Trying to spawn a basic setup in codemirror 6:

```auto
import("./haverbeke_2020_codemirror.js").then(({EditorState,EditorView})=>
new EditorView({state:EditorState.create({doc:""})}));

```

(the import resolution doesn’t even matter actually) and I get:

```auto
Uncaught ReferenceError: btoa is not defined
    at underline (VM1218 haverbeke_2020_codemirror.js:15010)
    at VM1218 haverbeke_2020_codemirror.js:15034
    at ModuleJob.run (VM95 module_job:146)
    at async Loader.import (VM93 loader:165)

```

the throwing function looks like this:

```auto
function underline(color) {
    let svg = `<svg xmlns="http://www.w3.org/2000/svg" width="6" height="3">
    <path d="m0 3 l2 -2 l1 0 l2 2 l1 0" stroke="${color}" fill="none" stroke-width=".7"/>
  </svg>`;
    return `url('data:image/svg+xml;base64,${btoa(svg)}')`;
}

```

btoa seems easily replaceable with Buffer.from,  
should this be a github issue, or am I missing  
something that there can be done?

My intention is basically to maintain a server-side  
state of a document during a collaborative session.

---

<div class="post-metadata">

**Author:** ![marijn](https://discuss.codemirror.net/user_avatar/discuss.codemirror.net/marijn/32/136_2.png) [@marijn](https://discuss.codemirror.net/u/marijn)\
**Post date:** [November 2, 2020, 8:20pm UTC](https://discuss.codemirror.net/t/working-in-jsdom-or-node-js-natively/138/10 "2020-11-02T20:20:56Z")

</div>

The `btoa` thing was fixed in [this patch](https://github.com/codemirror/codemirror.next/commit/043f8fb15345c011b95972fdd70a39a3192d6224) and should no longer be present in 0.14.0

---

<div class="post-metadata">

**Author:** ![bpstrngr](https://discuss.codemirror.net/user_avatar/discuss.codemirror.net/bpstrngr/32/4715_2.png) [@bpstrngr](https://discuss.codemirror.net/u/bpstrngr)\
**Post date:** [November 8, 2020, 8:18pm UTC](https://discuss.codemirror.net/t/working-in-jsdom-or-node-js-natively/138/11 "2020-11-08T20:18:03Z")

</div>

Oh right, I just had my package.json point to an earlier version. btoa went away, now with the same basic setup (and using JSDOM to set a window and document object on globalThis), I am getting

```auto
Uncaught ReferenceError: MutationObserver is not defined
    at new DOMObserver (VM1154 haverbeke_2020_codemirror.js:8874)
    at new EditorView (VM1154 haverbeke_2020_codemirror.js:9334)

```

the source looks like this:

```auto
class DOMObserver
{constructor(view, onChange, onScrollChanged) {
 this.view = view;
 this.onChange = onChange;
 this.onScrollChanged = onScrollChanged;
 [...]
 this.dom = view.contentDOM;
 this.observer = new MutationObserver(mutations=>{})
 [...]

```

Has anyone managed to get around MutationObserver?  
Due to my mentioned use case,  
I’m only going to run transactions against this view/state,  
not observed mutations so I could probably do without this part.

…actually, nevermind I realised I don’t need the View for this.  
now I just need to figure out how to shuffle transactions on a State.

---

<div class="post-metadata">

**Author:** ![marijn](https://discuss.codemirror.net/user_avatar/discuss.codemirror.net/marijn/32/136_2.png) [@marijn](https://discuss.codemirror.net/u/marijn)\
**Post date:** [November 8, 2020, 9:43pm UTC](https://discuss.codemirror.net/t/working-in-jsdom-or-node-js-natively/138/12 "2020-11-08T21:43:45Z")

</div>

> [@bpstrngr](#):
>
> I’m only going to run transactions against this view/state,

You can run transactions without having a view—`@codemirror/next/state` runs fine in node without JSDOM. But creating an actual view instance there is probably not going to work in any meaningful way.
