jsonnext / codemirror-json-schema

A JSONSchema enabled mode for codemirror 6, for json4 and json5, inspired by monaco-json
https://codemirror-json-schema.netlify.app
MIT License
57 stars 9 forks source link

Cannot import codemirror-json-schema #57

Closed boristian closed 11 months ago

boristian commented 11 months ago

Hi, I wanted to try this library but cannot import it.

When I try to import with

import { jsonSchema } from 'codemirror-json-schema'

But the browser console says:

client.js:194 Module not found: Error: Can't resolve './json-completion' in '<LOCAL_PATH>/node_modules/codemirror-json-schema/dist'
Did you mean 'json-completion.js'?
BREAKING CHANGE: The request './json-completion' failed to resolve only because it was resolved as fully specified (probably because the origin is strict EcmaScript Module, e. g. a module with javascript mimetype, a '*.mjs' file, or a '*.js' file where the package.json contains '"type": "module"').
The extension in the request is mandatory for it to be fully specified.Add the extension to the request.

I am using node 18 with webpack 5.

Am I doing something wrong?

Thanks in advance

acao commented 11 months ago

we don't ship a main entry yet but we probably should!

acao commented 11 months ago

@boristian try 0.4.3!

boristian commented 11 months ago

Thanks for your quick response. When using import I'm still getting the same error.

With

const { jsonSchema } = require('codemirror-json-schema')

I get

Uncaught ReferenceError: exports is not defined  at eval (index.js:17:23)
boristian commented 11 months ago

Sorry, 0.4.4 is also not working for me.

acao commented 11 months ago

@boristian sorry to hear - @imolorhe tried to reproduce the issue with webpack but couldn't - are you able to create a codesandbox that reproduces this bug? i have a hunch that the node version is a factor here

imolorhe commented 11 months ago

@boristian try the suggested solution here https://github.com/webpack/webpack/issues/11467#issuecomment-691873586

TLDR, add the following to your webpack config:

{
  test: /\.m?js/, // maybe change this regex to match codemirror-json-schema specifically e.g. /codemirror-json-schema/
  resolve: {
    fullySpecified: false
  }
}
boristian commented 11 months ago

Thanks for all your help, it woks now!

With version 0.4.4 and adding

{
  test: /codemirror-json-schema/, 
  resolve: { fullySpecified: false }
}

to the webpack rules everything is fine.