diegomura / react-pdf

📄 Create PDF files using React
https://react-pdf.org
MIT License
14.66k stars 1.16k forks source link

Can't resolve 'encoding' #2290

Open arobert93 opened 1 year ago

arobert93 commented 1 year ago

Describe the bug Next.js build outputs a warning message.

- warn Compiled with warnings

./node_modules/node-fetch/lib/index.js
Module not found: Can't resolve 'encoding' in './node_modules/node-fetch/lib'

Import trace for requested module:
./node_modules/node-fetch/lib/index.js
./node_modules/cross-fetch/dist/node-ponyfill.js
./node_modules/@react-pdf/font/lib/index.cjs.js
./node_modules/@react-pdf/renderer/lib/react-pdf.cjs.js
./src/app/dashboard/invoices/[invoiceId]/page.tsx

Expected behavior Build without any warning.

Desktop (please complete the following information):

NanaNiki commented 1 year ago

TL;DR npm add -D encoding solved my case

Hi, I've had a similar issue.

• Next.js 13.4.4

• React 18.2.0

• react-pdf/renderer 3.1.11

During dev i was getting 'just' a warning (everything was working fine):

- warn ./node_modules/node-fetch/lib/index.js
Module not found: Can't resolve 'encoding' in \node_modules\node-fetch\lib'
de-ponyfill.js
./node_modules/@react-pdf/font/lib/index.cjs.js
./node_modules/@react-pdf/renderer/lib/react-pdf.cjs.js
./app/components/--which were using @react-pdf

silly me ignored the warning, and proceed to run build, than only met with:

Failed to compile.

./node_modules/cross-fetch/dist/node-ponyfill.js
Module parse failed: Identifier 'Headers' has already been declared (20:13)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
| const _fetch = fetch
| export { _fetch as fetch }
> export const Headers = Headers
| export const Request = Request
| export const Response = Response

Import trace for requested module:
./node_modules/cross-fetch/dist/node-ponyfill.js
./node_modules/@react-pdf/font/lib/index.cjs.js
./node_modules/@react-pdf/renderer/lib/react-pdf.cjs.js
./app/components/--which were using @react-pdf

> Build failed because of webpack errors

and the solution:

In your next.config.js file, update the swcMinify to false

const nextConfig = { / all your config / swcMinify: false, }

didn't work for me, or any webpack configuration, or cross-fetch update or "downgrade", or --legacy-peer-deps with @react=pdf/renderer

but i've stumbled on this issue: Module not found: Can't resolve 'encoding' in '/vercel/path0/node_modules/cross-fetch/node_modules/node-fetch/lib' where solution was to run:

npm add -D encoding

and it work for my case with @react-pdf/renderer, i'm a freshie so i hope this helps