Skip to content

How import Codemirror.Next in NextJS #277

Closed
@jefer94

Description

@jefer94

Next: https://nextjs.org/

My code

import React, { useRef, useEffect, useState, ReactElement } from 'react'
import styled from 'styled-components'
import { EditorView } from '@codemirror/next/view'
import { EditorState } from '@codemirror/next/state'
import { lineNumbers } from '@codemirror/next/gutter'
import { defaultKeymap } from '@codemirror/next/commands'
import { javascript } from '@codemirror/next/lang-javascript'
import { oneDark } from '@codemirror/next/theme-one-dark'
// import { UnControlled as CodeMirror } from 'react-codemirror2'

// const Div = styled.div`
//   & > div {
//     height: ${(v) => v.height};
//     max-height: ${(v) => v.height};
//     outline: 0!important;
//     background-color: ${(v) => v.theme.surface};
//     color: ${(v) => v.theme.white};
//     font-size: ${(v) => v.theme.fontSize};
//     padding-left: 15px;
//   }
// `

type Props = {
  readonly className?: string
  readonly content: string
}

function CodemirrorWrapperBase({ className, content }: Props): ReactElement {
  const [loading, setLoading] = useState(true)
  const div = useRef<HTMLHeadingElement>()
  const editor = useRef<EditorView>()

  useEffect(() => {
    if (loading) {
      // eslint-disable-next-line functional/immutable-data
      editor.current = new EditorView({
        state: EditorState.create({
          doc: content,
          extensions: [lineNumbers(), javascript(), oneDark]
        }),
        dispatch: (v) => console.log('xxxxxxxxxx', v)
        // extensions: [keymap(defaultKeymap)]
        // extensions: []
        // extensions: [keymap(defaultKeymap), new GutterMarker()]
      })
      setLoading(false)
    }

    else if (div && div.current && editor.current) div.current.appendChild(editor.current.dom)
  }, [loading])

  return (
    <div className={className} ref={div} />
    // <CodeMirror
    //   value="<h1>I ♥ react-codemirror2</h1>"
    //   options={{
    //     mode: 'xml',
    //     theme: 'material',
    //     lineNumbers: true
    //   }}
    //   onChange={(editor, data, value) => {
    //     console.log(value)
    //   }}
    // />
  )
}

type CodemirrorWrapperProps = {
  readonly height: string
}

export const CodemirrorWrapper = styled(CodemirrorWrapperBase)`
  & > div {
    height: ${(v: CodemirrorWrapperProps) => v.height};
    max-height: ${(v: CodemirrorWrapperProps) => v.height};
    outline: 0!important;
    background-color: ${(v) => v.theme.surface};
    color: ${(v) => v.theme.white};
    font-size: ${(v) => v.theme.fontSize};
    padding-left: 15px;
  }

Error

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /node_modules/@codemirror/next/view/dist/index.js 
require() of ES modules is not supported.
require() of /node_modules/@codemirror/next/view/dist/index.js from /.next/server/static/development/pages/index.js is an ES module file as it is a .js file whose nearest parent package.
json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename /node_modules/@codemirror/next/view/dist/index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from /node_modules/@codemirror/next/view/package.json.

Next internally use Webpack, it only support CommonJS, but the dist folder is ES6, exist one example how use Codemirror.Next with NextJS or Webpack?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions