html-to-docx
html-to-docx copied to clipboard
Fails in Webpack5 / NextJS
Summary
Pretty simple, #68 doesn't work in Webpack5 / NextJS. Fails at import requiring fs.
Reproduction
import { saveAs } from 'file-saver'
import HTMLtoDOCX from 'html-to-docx'
import { useCallback } from 'react'
export default function SaveAsDocX(): JSX.Element | null {
const saveAsDocx = useCallback(async () => {
const htmlString = "<!DOCTYPE html><html><body><h1>Hello, World!</h1></body></html>"
const fileData = await HTMLtoDOCX(
htmlString,
null)
saveAs(fileData, 'Example.docx')
}, [])
return <button onClick={saveAsDocx}>
Download DocX
</button>
}
Results
Fails with the esm version (default import):
./node_modules/html-to-docx/dist/html-to-docx.esm.js:1:162
Module not found: Can't resolve 'fs'
Import trace for requested module:
./src/components/builder/SaveAsDocX.tsx
./src/components/builder/Builder.tsx
./pages/index.tsx
https://nextjs.org/docs/messages/module-not-found
Also fails with the umd version:
./node_modules/html-to-docx/dist/html-to-docx.umd.js:1:26
Module not found: Can't resolve 'fs'
Import trace for requested module:
./src/components/builder/SaveAsDocX.tsx
./src/components/builder/Builder.tsx
./pages/index.tsx
https://nextjs.org/docs/messages/module-not-found
Workaround (partial)
Workaround (but the package should just... not import fs)
/** @type {import('next').NextConfig} */
const nextConfig = {
// ...
webpack: (config) => {
config.resolve.fallback = {
...config.resolve.fallback,
fs: false,
}
return config
},
}
export default nextConfig
+1
+1
+1
+1
+1
@iamursky @lobnico @anton-liubushkin @devgeni @RonkyTang Please upvote the issue instead of notifying everyone subscribed to the issue with your +1 comments
this is much needed, any workaround?
+1
@AlbinoGeek after adding this workaround i am getting "TypeError: Cannot read properties of undefined (reading 'prototype')" this error in nextjs code is similar to you
@AlbinoGeek After adding workaround, did you get a warning like this?:
warn - ./node_modules/html-to-docx/dist/html-to-docx.esm.js Module not found: Can't resolve 'encoding' in '/home/zeelmodi/Documents/Projects/frontend/node_modules/html-to-docx/dist'
Can anyone suggest workaround for this as well?
I also get this warning.