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

Nextjs error - SyntaxError: Named export 'saveAs' not found. The requested module 'file-saver' is a CommonJS module #58

Open
stefan-krajnik opened this issue Oct 7, 2024 · 6 comments
Labels
bug Something isn't working

Comments

@stefan-krajnik
Copy link

stefan-krajnik commented Oct 7, 2024

Screenshot 2024-10-07 at 3 32 05 PM

Using dynamic import is not a solution, I need to be able to import extentions to generate html from JSON on server.

@hunghg255
Copy link
Owner

@stefan-krajnik how to can you get this error? can you share a repo?

@stefan-krajnik
Copy link
Author

@hunghg255 I don't have it anywhere publicly available, but it's easy to reproduce.

If you import the editor anywhere where it runs on server, e.g. pages/* or pages/api/* it crashes.
It only works if I separate it to a component which is then dynamically imported.

next 14.2.13

@stefan-krajnik
Copy link
Author

The issue is here

import { saveAs } from 'file-saver'

I'd recommend either to use another package or import the package asynchronously when save is triggered

So here

const wordDocument = docxSerializer.serialize(editor.state.doc, opts)
Packer.toBlob(wordDocument).then(blob => saveAs(new Blob([blob]), 'document.docx'))
return true

I'd do something like

const fileSaver = await import('file-saver')
const wordDocument = docxSerializer.serialize(editor.state.doc, opts) 
Packer.toBlob(wordDocument).then(blob => fileSaver.saveAs(new Blob([blob]), 'document.docx')) 
return true 

You can do the same even if you decide to use different library for saving files, it doesn't need to be part of the main bundle

@hunghg255
Copy link
Owner

The issue is here

import { saveAs } from 'file-saver'

I'd recommend either to use another package or import the package asynchronously when save is triggered

So here

const wordDocument = docxSerializer.serialize(editor.state.doc, opts)
Packer.toBlob(wordDocument).then(blob => saveAs(new Blob([blob]), 'document.docx'))
return true

I'd do something like

const fileSaver = await import('file-saver')
const wordDocument = docxSerializer.serialize(editor.state.doc, opts) 
Packer.toBlob(wordDocument).then(blob => fileSaver.saveAs(new Blob([blob]), 'document.docx')) 
return true 

You can do the same even if you decide to use different library for saving files, it doesn't need to be part of the main bundle

oke, I got it, I will check late-night

@hunghg255 hunghg255 added the bug Something isn't working label Oct 8, 2024
@hunghg255
Copy link
Owner

@stefan-krajnik FYI: I resolved this bug,
the problem now is Shiki package =))) I will re-check

@stefan-krajnik
Copy link
Author

@hunghg255 any progress?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants