I’m having trouble setting up pdfjs-dist with Webpack in my Django project. I switched from link-based pdf.js to npm’s pdfjs-dist for better stability. But I’m stuck with compilation errors.
Here’s what I’m seeing:
WARNING in ./node_modules/worker-loader/dist/index.js
Module not found: Error: Can't resolve 'webpack/lib/web/FetchCompileAsyncWasmPlugin' in '/path/to/node_modules/worker-loader/dist'
// ... more errors ...
I thought pdfjs-dist was supposed to work out of the box without extra config. This code should work, but it’s not compiling:
I encountered a similar issue when integrating pdfjs-dist with Webpack in a Django project. One solution that worked for me was updating the import statement. Instead of importing from ‘pdfjs-dist/webpack’, try importing directly from ‘pdfjs-dist’:
This approach bypasses some of the Webpack-specific issues. Also, ensure you’re using compatible versions of pdfjs-dist, Webpack, and worker-loader. Upgrading to the latest stable versions of each might resolve compatibility conflicts.
If issues persist, consider adding a resolve alias in your Webpack config:
I’ve faced a similar issue before when integrating pdfjs-dist within a Django project using Webpack. In my experience, the problem often boils down to a version mismatch between Webpack and worker-loader. One thing to verify is that your Webpack version is compatible with the worker-loader version you’re using—Webpack 5.x tends to work best with the latest compatible worker-loader.
I also had success by explicitly configuring Webpack to handle Web Workers. For instance, adding the following to your config:
This configuration directs Webpack on how to manage the PDF.js worker file. Additionally, I found that importing directly from ‘pdfjs-dist’ rather than ‘pdfjs-dist/webpack’ avoided some compilation issues.