Vue CodeMirror 6 Component
vue-codemirror is a Vue 3 component that provides a robust wrapper for CodeMirror 6, the modern code editor library. The current stable version is 6.1.1. This library is actively maintained, with frequent minor releases and bug fixes, indicated by its release history. A key differentiator is its strict adherence to Vue 3 and CodeMirror 6, making it completely incompatible with previous versions of CodeMirror (v5 and below) or Vue (v2). It leverages CodeMirror 6's native ES Module structure, meaning it no longer supports UMD modules for direct browser references, requiring a module bundler. Users must explicitly install additional CodeMirror language, theme, and state packages as needed, as they are not bundled.
Common errors
-
Failed to resolve component: codemirror
cause The Codemirror component was not properly imported or registered in your Vue component.fixEnsure you have `import { Codemirror } from 'vue-codemirror'` and registered it in your component's `components` option: `components: { Codemirror }`. -
Module not found: Error: Can't resolve 'codemirror' in 'your-project-path/node_modules/vue-codemirror/dist'
cause The `codemirror` peer dependency is missing from your project's `node_modules`.fixRun `npm install codemirror` or `yarn add codemirror` to install the core CodeMirror library. -
Uncaught SyntaxError: require is not defined in ES module scope
cause Attempting to use CommonJS `require()` syntax to import `vue-codemirror` or its dependencies, but the library is ESM-only since v5/v6.fixUpdate your import statements to use ES Module `import` syntax: `import { Codemirror } from 'vue-codemirror'`. -
TypeError: Cannot read properties of undefined (reading 'of')
cause Likely an issue with CodeMirror extensions. You might be passing an undefined or invalid extension to the `extensions` prop, or a required CodeMirror package for an extension is missing.fixVerify that all CodeMirror language, theme, and utility packages (e.g., `@codemirror/lang-javascript`) are installed, correctly imported, and properly added to the `extensions` array, e.g., `[javascript()]`.
Warnings
- breaking Versions 5 and 6 of vue-codemirror are completely incompatible with previous versions (v4 and below). This is due to a fundamental rewrite based on CodeMirror 6 and Vue 3. Migration from v4 will require significant code changes.
- gotcha CodeMirror 6, and consequently vue-codemirror v5/v6, are developed with native ES Modules and do not support direct browser references to UMD modules. A module bundler like Webpack or Vite is required.
- gotcha Language support, themes, and other CodeMirror features are not bundled with vue-codemirror. You must explicitly install separate `@codemirror/*` packages (e.g., `@codemirror/lang-javascript`, `@codemirror/theme-one-dark`) based on your application's needs.
- gotcha If you need to import specific CodeMirror API interfaces or types (e.g., `EditorState`, `EditorView`) from `@codemirror/*` packages, those specific packages must be explicitly listed in your project's `dependencies` in `package.json`, even if they are transitively pulled in.
Install
-
npm install vue-codemirror -
yarn add vue-codemirror -
pnpm add vue-codemirror
Imports
- Codemirror
const Codemirror = require('vue-codemirror')import { Codemirror } from 'vue-codemirror' - javascript
import { langJavascript } from 'vue-codemirror'import { javascript } from '@codemirror/lang-javascript' - EditorState
import { EditorState } from 'codemirror'import { EditorState } from '@codemirror/state'
Quickstart
import { defineComponent, ref, shallowRef } from 'vue'
import { Codemirror } from 'vue-codemirror'
import { javascript } from '@codemirror/lang-javascript'
import { oneDark } from '@codemirror/theme-one-dark'
export default defineComponent({
components: {
Codemirror
},
setup() {
const code = ref(`console.log('Hello, vue-codemirror!');\nfunction sum(a, b) { return a + b; }`);
const extensions = [javascript(), oneDark];
const view = shallowRef();
const handleReady = (payload) => {
view.value = payload.view;
console.log('CodeMirror is ready:', payload);
};
const log = (event, value) => {
console.log(`${event} event:`, value);
};
return {
code,
extensions,
handleReady,
log
};
},
template: `
<div style="height: 400px; border: 1px solid #ccc;">
<codemirror
v-model="code"
placeholder="Code goes here..."
:style="{ height: '100%' }"
:autofocus="true"
:indent-with-tab="true"
:tab-size="2"
:extensions="extensions"
@ready="handleReady"
@change="log('change', $event)"
@focus="log('focus', $event)"
@blur="log('blur', $event)"
/>
</div>
`
})