Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cant get this working in Svelte Kit #96

Closed
MarcGodard opened this issue Jun 27, 2021 · 34 comments
Closed

Cant get this working in Svelte Kit #96

MarcGodard opened this issue Jun 27, 2021 · 34 comments
Labels

Comments

@MarcGodard
Copy link

Hi I can't seem to get this working in Svelte Kit.

Like all my other modules:

[vite] Error when evaluating SSR module /node_modules/remark-parse/index.js?v=94403ef9:
ReferenceError: module is not defined
    at /node_modules/remark-parse/index.js?v=94403ef9:3:1
    at instantiateModule (/Users/marcgodard/Documents/Github/swp-crm/node_modules/vite/dist/node/chunks/dep-0ed4fbc0.js:69982:166)

And If I turn SSR off I get:

[vite] Internal server error: Cannot find module './transformers/module'

Has anyone gotten this working yet?

@muellercornelius
Copy link

Yep I have the same problem ;(

@VirgileD
Copy link

Yep, same here

module is not defined

ReferenceError: module is not defined
    at /node_modules/lodash.debounce/index.js?v=5d239c0d:373:1
    at instantiateModule (/home/virgiled/Projects/lookout-radar/wl-vision-1/node_modules/vite/dist/node/chunks/dep-c1a9de64.js:73464:166)

@CaptainCodeman
Copy link

It's very frustrating - this looks like a fantastic editor, and it's written using Svelte - but trying to consume it directly from SvelteKit seems to be impossible.

Even more frustrating is if you drop the source directly into the "lib" folder of a brand new SvelteKit project it all just works. Using the "package" feature of SvelteKit then makes a version that can be imported into other SvelteKit projects, so maybe using this as a packaging option would be an idea?

@benwoodward
Copy link

Would also love to see this issue resolved, just upgraded my project to SvelteKit and now it seems like my editor doesn't work.

@axmad386
Copy link

Hi, I encounter the same problem here. I think I know what the source of the problem. Svelte Kit currently work on ESM. So importing cjs module with default import will break the svelte-kit
for example
import debounce from "lodash.debounce" will break
but import * as debounce from "lodash.debounce" is OK.
I try to modif the util.js and refactor the import way, but I ended up with this

import ghSchema from 'hast-util-saniretize/lib/github.json';
I don't understand what is hast-util-saniretize, I cannot find it in npm

@benwoodward
Copy link

import ghSchema from 'hast-util-saniretize/lib/github.json';
I don't understand what is hast-util-saniretize, I cannot find it in npm

You have a typo. This is what you're looking for:

https://github.com/syntax-tree/hast-util-sanitize/blob/4dd66cc8ae4066f58627359f6c53b45abdadfebe/lib/github.json

You need to install hast-util-sanitize v3.0.2

@axmad386
Copy link

Oh I make a mistake, I suddenly edited the import string. OK I'll try again
Thanks @benwoodward

@axmad386
Copy link

Now stuck on this

The requested module '/node_modules/.pnpm/unist-util-visit@2.0.3/node_modules/unist-util-visit/index.js?v=421b96cf' does not provide an export named 'default'

Don't know where this module is imported, maybe I miss something.
Waiting for the maintainer to resolve this Issue 😞

@benwoodward
Copy link

benwoodward commented Sep 11, 2021

Yeah, I had to upgrade it to "^4.0.0" to get rid of that error. Most, if not all of the syntax-tree plugins need to be upgraded to their latest versions to work with Sveltekit—this is due to the fact that Vite only works with ESM modules and syntax-tree converted all their plugins to ESM modules only in recent versions.

@axmad386
Copy link

Yeah, I had to upgrade it to "^4.0.0" to get rid of that error. Most, if not all of the syntax-tree plugins need to be upgraded to their latest versions to work with Sveltekit—this is due to the fact that Vite only works with ESM modules and syntax-tree converted all their plugins to ESM modules only in recent versions.

Which module you upgraded to ^4.0.0?

@benwoodward
Copy link

unist-util-visit needs to be ^4.0.0

@axmad386
Copy link

Ok, thanks for the advice @benwoodward 😊

@MarcGodard
Copy link
Author

Re-posting original issue due to thread pollution:

Hi I can't seem to get this working in Svelte Kit.

Like all my other modules:

[vite] Error when evaluating SSR module /node_modules/remark-parse/index.js?v=94403ef9:
ReferenceError: module is not defined
    at /node_modules/remark-parse/index.js?v=94403ef9:3:1
    at instantiateModule (/Users/marcgodard/Documents/Github/swp-crm/node_modules/vite/dist/node/chunks/dep-0ed4fbc0.js:69982:166)

And If I turn SSR off I get:

[vite] Internal server error: Cannot find module './transformers/module'

Has anyone gotten this working yet?

@axmad386
Copy link

Hi @MarcGodard, sorry for make pollution to your thread. I think you have 2 issue here.
First

[vite] Error when evaluating SSR module /node_modules/remark-parse/index.js?v=94403ef9:
ReferenceError: module is not defined
    at /node_modules/remark-parse/index.js?v=94403ef9:3:1
    at instantiateModule (/Users/marcgodard/Documents/Github/swp-crm/node_modules/vite/dist/node/chunks/dep-0ed4fbc0.js:69982:166)

This caused by ESM problem like we discuss above. Remark parse is cjs module. It cannot work on vite (svelte-kit). So to make this package work we must change how the way cjs module imported or use the esm module version of the module (if exists).
Not only remark-parse, the following module is also cjs module: lodash.debounce, lodash.throttle, remark-rehype, etc. So we just wait for maintainer update their dependency from cjs to esm version or change how the module imported
For now, I try to copy this bytemd from node_modules to src/vendor and change the code and update the dependency manually.

Second

[vite] Internal server error: Cannot find module './transformers/module'

May be this is different Issue. I cannot reproduce this in my project even I turn off ssr mode.

@hhaoao
Copy link

hhaoao commented Oct 31, 2021

In an SSR environment, I can't even start editor

Error: <Editor> is not a valid SSR component. You may need to review your build config to ensure that dependencies are compiled, rather than imported as pre-compiled modules

#87

@pd4d10
Copy link
Owner

pd4d10 commented Nov 21, 2021

Hi all, thanks for providing so much context for this issue!

I have provided an example here, which is built with the latest version of SvelteKit (with tag next). Hope this can help

@outerlook
Copy link

Does this still work? Vite is always surprising on this unstable phase.

I'm getting:
Cannot find module 'codemirror-ssr/package.json' imported from '/home/project/node_modules/.pnpm/bytemd@1.10.13_supports-color@9.0.2/node_modules/bytemd/lib/@root'

for modules

  • lodash.debounce
  • lodash.throttle
  • codemirror-ssr
    I was getting rid of the error by installing each of these deps. But I guess that isn't correct, right?

I'll try the minimum repo soon, but I'll put it here if someone else is facing it.

		"@sveltejs/kit": "1.0.0-next.202",
		"@bytemd/plugin-gfm": "1.10.13",
		"bytemd": "1.10.13"

@MarcGodard
Copy link
Author

I can't get that example working in my code either.

When i build the app, it shows up (not in development at all).

Screen Shot 2021-12-20 at 5 34 55 PM

When i type something, i get these errors (useless errors because its built):

TypeError: Rj is not a function
    Pj http://localhost:3000/_app/chunks/vendor-b465ee4f.js:59
    rW http://localhost:3000/_app/chunks/vendor-b465ee4f.js:66
    Fj http://localhost:3000/_app/chunks/vendor-b465ee4f.js:59
    SW http://localhost:3000/_app/chunks/vendor-b465ee4f.js:66
    t http://localhost:3000/_app/chunks/vendor-b465ee4f.js:67
    i http://localhost:3000/_app/chunks/vendor-b465ee4f.js:57
    c http://localhost:3000/_app/chunks/vendor-b465ee4f.js:57
    o http://localhost:3000/_app/chunks/vendor-b465ee4f.js:57
    u http://localhost:3000/_app/chunks/vendor-b465ee4f.js:57
    i http://localhost:3000/_app/chunks/vendor-b465ee4f.js:57
    c http://localhost:3000/_app/chunks/vendor-b465ee4f.js:57
    o http://localhost:3000/_app/chunks/vendor-b465ee4f.js:57
    u http://localhost:3000/_app/chunks/vendor-b465ee4f.js:57
    i http://localhost:3000/_app/chunks/vendor-b465ee4f.js:57
    c http://localhost:3000/_app/chunks/vendor-b465ee4f.js:57
    n http://localhost:3000/_app/chunks/vendor-b465ee4f.js:57
    I http://localhost:3000/_app/chunks/vendor-b465ee4f.js:57
    w http://localhost:3000/_app/chunks/vendor-b465ee4f.js:57
    xD http://localhost:3000/_app/chunks/vendor-b465ee4f.js:57
    i http://localhost:3000/_app/chunks/vendor-b465ee4f.js:57
    c http://localhost:3000/_app/chunks/vendor-b465ee4f.js:57
    o http://localhost:3000/_app/chunks/vendor-b465ee4f.js:57
    u http://localhost:3000/_app/chunks/vendor-b465ee4f.js:57
    i http://localhost:3000/_app/chunks/vendor-b465ee4f.js:57
    c http://localhost:3000/_app/chunks/vendor-b465ee4f.js:57
    n http://localhost:3000/_app/chunks/vendor-b465ee4f.js:57
    $ http://localhost:3000/_app/chunks/vendor-b465ee4f.js:57
    C http://localhost:3000/_app/chunks/vendor-b465ee4f.js:57
    M http://localhost:3000/_app/chunks/vendor-b465ee4f.js:57
    update http://localhost:3000/_app/chunks/vendor-b465ee4f.js:79
    v7 http://localhost:3000/_app/chunks/vendor-b465ee4f.js:4
    lb http://localhost:3000/_app/chunks/vendor-b465ee4f.js:4
    promise callback*sb http://localhost:3000/_app/chunks/vendor-b465ee4f.js:4
    b7 http://localhost:3000/_app/chunks/vendor-b465ee4f.js:4
    ctx http://localhost:3000/_app/chunks/vendor-b465ee4f.js:4
    he http://localhost:3000/_app/chunks/vendor-b465ee4f.js:139
    O http://localhost:3000/_app/chunks/vendor-b465ee4f.js:52
    $ http://localhost:3000/_app/chunks/vendor-b465ee4f.js:52
    x http://localhost:3000/_app/chunks/vendor-b465ee4f.js:52
    setTimeout handler*x http://localhost:3000/_app/chunks/vendor-b465ee4f.js:52
    setTimeout handler*C http://localhost:3000/_app/chunks/vendor-b465ee4f.js:52
    Y http://localhost:3000/_app/chunks/vendor-b465ee4f.js:52
    update http://localhost:3000/_app/chunks/vendor-b465ee4f.js:139
    v7 http://localhost:3000/_app/chunks/vendor-b465ee4f.js:4
    lb http://localhost:3000/_app/chunks/vendor-b465ee4f.js:4
vendor-b465ee4f.js:79:5040
    update http://localhost:3000/_app/chunks/vendor-b465ee4f.js:79
    v7 http://localhost:3000/_app/chunks/vendor-b465ee4f.js:4
    lb http://localhost:3000/_app/chunks/vendor-b465ee4f.js:4
    (Async: promise callback)
    sb http://localhost:3000/_app/chunks/vendor-b465ee4f.js:4
    b7 http://localhost:3000/_app/chunks/vendor-b465ee4f.js:4
    ctx http://localhost:3000/_app/chunks/vendor-b465ee4f.js:4
    he http://localhost:3000/_app/chunks/vendor-b465ee4f.js:139
    O http://localhost:3000/_app/chunks/vendor-b465ee4f.js:52
    $ http://localhost:3000/_app/chunks/vendor-b465ee4f.js:52
    x http://localhost:3000/_app/chunks/vendor-b465ee4f.js:52
    (Async: setTimeout handler)
    x http://localhost:3000/_app/chunks/vendor-b465ee4f.js:52
    (Async: setTimeout handler)
    C http://localhost:3000/_app/chunks/vendor-b465ee4f.js:52
    Y http://localhost:3000/_app/chunks/vendor-b465ee4f.js:52
    update http://localhost:3000/_app/chunks/vendor-b465ee4f.js:139
    v7 http://localhost:3000/_app/chunks/vendor-b465ee4f.js:4
    lb http://localhost:3000/_app/chunks/vendor-b465ee4f.js:4

When i run in dev (and nothing shows up):

5:38:23 p.m. [vite] Error when evaluating SSR module /node_modules/lodash.debounce/index.js?v=3bac8306:
ReferenceError: module is not defined
    at /node_modules/lodash.debounce/index.js?v=3bac8306:377:1
    at instantiateModule (/Users/marcgodard/Documents/Github/crm/node_modules/vite/dist/node/chunks/dep-7817f5b4.js:67424:15)
5:38:23 p.m. [vite] Error when evaluating SSR module /node_modules/bytemd/lib/editor.svelte:
ReferenceError: module is not defined
    at /node_modules/lodash.debounce/index.js?v=3bac8306:375:1
    at instantiateModule (/Users/marcgodard/Documents/Github/crm/node_modules/vite/dist/node/chunks/dep-7817f5b4.js:67424:15)
5:38:23 p.m. [vite] Error when evaluating SSR module /node_modules/bytemd/lib/index.js:
ReferenceError: module is not defined
    at /node_modules/lodash.debounce/index.js?v=3bac8306:373:1
    at instantiateModule (/Users/marcgodard/Documents/Github/crm/node_modules/vite/dist/node/chunks/dep-7817f5b4.js:67424:15)
5:38:23 p.m. [vite] Error when evaluating SSR module /src/routes/admin/test/index.svelte:
ReferenceError: module is not defined
    at /node_modules/lodash.debounce/index.js?v=3bac8306:371:1
    at instantiateModule (/Users/marcgodard/Documents/Github/crm/node_modules/vite/dist/node/chunks/dep-7817f5b4.js:67424:15)
module is not defined
ReferenceError: module is not defined
    at /node_modules/lodash.debounce/index.js?v=3bac8306:369:1
    at instantiateModule (/Users/marcgodard/Documents/Github/crm/node_modules/vite/dist/node/chunks/dep-7817f5b4.js:67424:15)

@magick93
Copy link

Does anyone have any suggestions on how to fix this?

It seems there is a lot of interest in using this with Sveltekit.

@techydad05
Copy link

anyone ever able to get this working? Im having the same issue module not found with the lodash.debounce.

@sykire
Copy link

sykire commented Feb 5, 2022

Had the same problem while importing remark-parse, remark-rehype and rehype-highlight on Astro.

remark-parse required debug which is not esm
remark-rehype required mdurl, same problem
rehype-highlight required highlight.js, same

So I had to include them on the pre-bundle with the optimizeDeps.include vite option:

{
  optimizeDeps: {
    include: ["debug", "mdurl", "highlight.js"]
  }
}

@benmccann
Copy link

There seems to be a few issues here:

@pd4d10
Copy link
Owner

pd4d10 commented Mar 1, 2022

@benmccann Thanks for the summary!

Based on the above feedback from our users, I think one of the most likely reasons may be the version dependency. The unified ecosystem packages consist of many small packages with long dependency chains, which cause the inconsistent nested package versions installed by users, while this demo works fine because the lock file in this repo happens to have the right version.

The versions we used are mainly outdated, for example, unifed and remark/rehype ecosystem packages, while the latest versions have been upgraded to ESM only. But we can't just update them to the latest versions, because it will break the users who use them directly in Node.js in CJS, for example, some users may call require('bytemd').getProcessor to convert Markdown to HTML.

So in the next minor release, I think it might be a better choice to bundle all the dependencies into it, so there won't be any version inconsistency problems for our users. Already working on it, will be done in a few weeks

@pd4d10
Copy link
Owner

pd4d10 commented Mar 1, 2022

this library isn't being packaged in the recommended manner

Thanks for providing the guide. It's because the latest main branch is WIP, which is not ready for production.

@MarcGodard
Copy link
Author

Thanks everyone. I think it is invalid packaging too.

@pd4d10
Copy link
Owner

pd4d10 commented Mar 25, 2022

Hi all, sorry for the late fix.

Could you please help to give the latest version a try?

@pd4d10 pd4d10 reopened this Mar 26, 2022
@Noxsios
Copy link

Noxsios commented Mar 27, 2022

For me, I got it working using the vanilla JS config and adding bytemd to optimizeDeps. Not ideal, but it works.

@pd4d10
Copy link
Owner

pd4d10 commented Mar 27, 2022

Also added a Sveltekit example here: https://github.com/bytedance/bytemd/tree/main/examples/svelte

@Noxsios
Copy link

Noxsios commented Mar 27, 2022

After upgrading to bytemd@^1.12.4 Sveltekit is working w/o needing to use VanillaJS config or optimizeDeps. Thanks.

@FunMiles
Copy link

FunMiles commented Apr 3, 2022

@pd4d10 Your example works as standalone, but when I add

    "@bytemd/plugin-gfm": "latest",
    "bytemd": "latest",

and put your index.js as a route in an existing code, I get

Error: <Editor> is not a valid SSR component. You may need to review your build config to ensure that dependencies are compiled, rather than imported as pre-compiled modules

I am not understanding what is different in your example and in my code. Any idea what I may have missed?

@pd4d10
Copy link
Owner

pd4d10 commented Apr 4, 2022

@pd4d10 Your example works as standalone, but when I add


    "@bytemd/plugin-gfm": "latest",

    "bytemd": "latest",

and put your index.js as a route in an existing code, I get


Error: <Editor> is not a valid SSR component. You may need to review your build config to ensure that dependencies are compiled, rather than imported as pre-compiled modules

I am not understanding what is different in your example and in my code. Any idea what may have missed?

Could you please provide a minimal reproduction repo?

@MarcGodard
Copy link
Author

I will test this this coming weekend and close issue if resolved. Thanks for the fix.

@github-actions
Copy link
Contributor

github-actions bot commented Jun 4, 2022

This issue is stale because it has been open for 60 days with no activity.

@tosinek
Copy link

tosinek commented Feb 20, 2023

While it works in development, I cannot start the deployed app, using MeteorJS. I think it is related to this issue.

Error [ERR_REQUIRE_ESM] [ERR_REQUIRE_ESM]: Must use import to load ES Module: /built_app/programs/server/npm/node_modules/codemirror-ssr/lib/codemirror.js
        require() of ES modules is not supported.
        require() of /built_app/programs/server/npm/node_modules/codemirror-ssr/lib/codemirror.js from /built_app/programs/server/npm/node_modules/bytemd/svelte/editor.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 codemirror.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from /built_app/programs/server/npm/node_modules/codemirror-ssr/package.json.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests