tiptap: [Bug]: dynamic usage of require is not supported

Affected Packages

html

Version(s)

3.0.0-beta.7

Bug Description

In Next.js server functions

If I use Next.js with Turbopack

When I call the generateHTML function or the generateJSON function

This error is thrown:

Error: dynamic usage of require is not supported

Browser Used

Chrome

Code Example URL

No response

Expected Behavior

The error is not thrown

Additional Context (Optional)

I think the error is due to this dynamic require call:

https://github.com/ueberdosis/tiptap/blob/699b463e52aa1da9e24052fadf1eb89aefbe20d8/packages/html/src/createSafeWindow.ts#L14

I think the dynamic require is made on purpose so that the happy-dom-without-node library is not loaded in the browser. But then how do we make it work with Next.js?

Dependency Updates

  • Yes, I’ve updated all my dependencies.

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Reactions: 2
  • Comments: 24 (12 by maintainers)

Most upvoted comments

I will check again if there’s no other way except making it async (for example another happy-dom package that doesn’t nuke node internals lol).

Last resort will probably be a breaking change with async/await syntax

I think this is the issue https://github.com/vercel/next.js/issues/56531

A possible solution would be to use import instead of require, the downside is that this would make the generateHTML and generateJSON functions async.

This separation of server and non-server generateJSON implementations is proving to be a big PITA.

Refactored/duplicated already dozens of files and there seemingly is no end to how deeply this is going to go.

@bdbch why does /server entry still using dynamic import if now it is dedicated to server?

This seems to unnecessarily turn the function to async.

Okay I’m working on it right now - doing my final tests.

Current implementation looks like this - the default @tiptap/html import will only be used for client side environments where window and the browser API is accessible. That way we won’t need any happy-dom that mocks NodeJS API’s.

There will be an additional, code-splittable import @tiptap/html/server that will provide async packages which will lazyload happy-dom on demand.

I’m doing local node tests now. Will come back later.

As reported here (with a reproduction example), this is happening in both cases where the turbopack is enabled and disabled. So, it’s not a turbopack issue: https://github.com/ueberdosis/tiptap/issues/6502

Making it async is a pretty reasonable option in my opinion. That way, we should be able to use generateHTML with NextJS async (server) components.

I’m not sure, I like the fact that it’s not async. I think the issue is more with Turbopack. I’ll research why turbopack does not support dynamic require.

I am getting same error with Vite/Rollup.