3

I am implementing the CodeMirror to one of the textarea in my Nuxtjs/Vuejs application. I would like to beautify the textarea as per the XML.

Sometimes the CodeMirror works perfectly but sometimes when I reload the page I get the error:

Test.vue
33:18  error  'CodeMirror' is not defined  no-under

So initially it works perfectly but when I try to make some changes to any file in the project and when the Nuxtjs/Vuejs server reloads again to incorporate the new changes then I get the error error 'CodeMirror' is not defined

I am not understanding why do I get the error sometimes and I do not get it some other time. As I have added the required CDN and done the steps mentioned in various answers and articles, I would expect that it does not throw the error at all. Can someone please help me with this issue?

Steps followed:

  1. Added the CDN to my nuxt-config.js: Scripts:
    script: [
      {
        src:"text/javascript",
        src:"https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.32.0/codemirror.min.js"
      },
      {
        src:"text/javascript",
        src:"https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.32.0/mode/xml/xml.min.js"
      }
    ],

CSS:

{
 rel: "stylesheet",
 href:"https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.63.1/codemirror.min.css"
}

Following is my Test.vue:

<template>
  <div>
    <div class="row">
      <div class="col-md-5">
        <div class="row">
          <div class="col-md-12">
            <textarea
              id="test"
              v-model="xmlInput"
              class="form-control"
              placeholder="XML Document"
              spellcheck="false"
              data-gramm="false"
              @input="convertToJSON()"
            />
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      xmlInput: ''
    }
  },
  methods: {
    convertToJSON () {
      console.log('ONE')
      const cm = CodeMirror.fromTextArea(document.getElementById('test'), {
        mode: 'application/xml',
        lineNumbers: true,
        matchBrackets: true,
        styleActiveLine: true,
        lineWrapping: true,
        tabSize: 2,
        value: 'console.log("Hello, World");'
      })
      cm.setSize(500, 500)
    }
  }
}
</script>

<style scoped>
textarea {
  height: 78vh;
  white-space: nowrap;
  resize: both;
}

::-webkit-input-placeholder {
  color: #f1948a;
  text-align: center;
}
</style>

Can someone please help me out with this issue? What am I doing wrong here? Any suggestions would be really appreciated. Thanks in advance.

Sandbox for re-creating issue: https://codesandbox.io/s/boring-water-g14zd?file=/pages/index.vue

Error in Sandbox: enter image description here

BATMAN_2008
  • 2,788
  • 3
  • 31
  • 98
  • This might help you - https://stackoverflow.com/questions/67360602/javascript-using-codemirror-js-error-uncaught-referenceerror-codemirror-is-not – Mohib Arshi Oct 04 '21 at 05:03
  • @MohibArshi Thanks a lot for your response. I tried that but it does not seem to work for me and the error still persists. I do not have any `type` parameter for my `Scripts` and `CSS` has been marked with `rel:stylesheet` so as per the answer this should work. But its not working for me. Can you please suggest me something? – BATMAN_2008 Oct 04 '21 at 05:16
  • There is a type property on nuxt scripts - ```{ src: "..." type: "text/javascript" }``` – Mohib Arshi Oct 04 '21 at 06:03
  • @MohibArshi Thanks a lot for your response. I changed it to `type` but still getting the error. Initially, it worked but when I do force-load the page then it starts throwing the error again. It works sometimes and sometimes it does not work. Not sure what's going wrong here. Any help please? – BATMAN_2008 Oct 04 '21 at 06:23
  • @MohibArshi I tried few things and found out that the problem is happening when I make the changes to some files and when the `nuxtjs/Vuejs` reloads to adapt the changes that's when I get this error. So initially it works perfectly but when I try to make some changes to the file and when the `Nuxtjs/Vuejs` server reloads then I get the error `error 'CodeMirror' is not defined ` – BATMAN_2008 Oct 04 '21 at 07:00
  • So I tested the code on codesandbox and initially it was throwing ```'CodeMirror' is not defined``` error. But i added ```"defer": true``` to the script and it seems to be working fine. Check this out - https://codesandbox.io/s/falling-cdn-5o0t6?file=/nuxt.config.js – Mohib Arshi Oct 04 '21 at 10:03
  • @MohibArshi Thanks a lot for taking your time and checking the problem. Actually, for some reason, it still throws the same error for me in my application as well as in the Sandbox. For sandbox, In `Index.vue` file it shows `redline` below the `CodeMirror` and when I hover it shows `CodeMirror` is not defined. Also, in actual application throws the same error event after adding the `defer:true`. Can you please have a look and provide some solution to fix this? Sandbox: https://codesandbox.io/s/boring-water-g14zd?file=/pages/index.vue. I have also attached the error above. – BATMAN_2008 Oct 04 '21 at 10:43
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/237780/discussion-between-mohib-arshi-and-batman-2008). – Mohib Arshi Oct 04 '21 at 10:59

1 Answers1

3

You can do the following things to make it work.

  • First, you don't have to re-initialize the codemirror editor on each input change @input="convertToJSON()"
  • You may initialize the editor when the component mounts.

Code

<template>
  <div>
    <div class="row">
      XML Dat
      <div class="col-md-5">
        <div class="row">
          <div class="col-md-12">
            <textarea
              id="test"
              v-model="xmlInput"
              class="form-control"
              placeholder="XML Document"
              spellcheck="false"
              data-gramm="false"
            />
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
export default {
  data() {
    return {
      xmlInput: "",
    };
  },
  mounted() {
    // https://stackoverflow.com/questions/53981928/using-codemirror-cannot-set-property-modeoption-of-undefined
    const editor = document.getElementById("test");
    editor.value = "";
    /** eslint-next-line */
    const cm = CodeMirror.fromTextArea(editor, {
      mode: "application/xml",
      lineNumbers: true,
      matchBrackets: true,
      styleActiveLine: true,
      lineWrapping: true,
      tabSize: 2,
      value: 'console.log("Hello, World");',
    });

    cm.setSize(500, 500);
  },
};

Here is the working codesandbox link - https://codesandbox.io/s/falling-cdn-5o0t6?file=/pages/index.vue

If you want to use codemirror npm module, here is the link for the working codesandbox for that - https://codesandbox.io/s/boring-water-g14zd?file=/pages/index.vue

Mohib Arshi
  • 830
  • 4
  • 13
  • 1
    Thanks a lot for your efforts and response. This is working as expected. You deserve the accepted answer :) Thanks a lot again for helping and have a nice day :) – BATMAN_2008 Oct 04 '21 at 18:44