html-to-docx icon indicating copy to clipboard operation
html-to-docx copied to clipboard

Fails in Webpack5 / NextJS

Open AlbinoGeek opened this issue 3 years ago • 11 comments

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

AlbinoGeek avatar Apr 17 '22 15:04 AlbinoGeek

+1

iamursky avatar May 09 '22 09:05 iamursky

+1

lobnico avatar May 09 '22 20:05 lobnico

+1

anton-liubushkin avatar May 13 '22 08:05 anton-liubushkin

+1

devgeni avatar May 13 '22 09:05 devgeni

+1

RonkyTang avatar Dec 07 '22 03:12 RonkyTang

@iamursky @lobnico @anton-liubushkin @devgeni @RonkyTang Please upvote the issue instead of notifying everyone subscribed to the issue with your +1 comments

noverby avatar Dec 07 '22 08:12 noverby

this is much needed, any workaround?

Wamy-Dev avatar Feb 19 '23 23:02 Wamy-Dev

+1

andreeapurta avatar Sep 03 '23 09:09 andreeapurta

@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

gauravsapkal1 avatar Jan 06 '24 14:01 gauravsapkal1

@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?

zeelmodi-simform avatar Jan 30 '24 07:01 zeelmodi-simform

I also get this warning.

Milos5611 avatar Mar 27 '24 09:03 Milos5611