13👍
This issue seems to arise due to esModule
option introduced in worker-loader@3.0.0
.
The fix for this was merged in (pre-release) pdjs-dist@2.6.347
You can fix this by either upgrading pdfjs-dist
to v2.6.347
OR downgrading worker-loader
to v2.0.0
7👍
I just had to solve this issue myself…
This issue
Module not found: Error: Can't resolve 'module' in '/home/giampaolo/dev/KJ_import/KJ-JS/node_modules/webpack/lib/node'
Is caused by worker-loader
loading NodeTargetPlugin
, which in turn runs require("module")
which I think (but I’m not 100%) is for native node modules, which when running Webpack targeted for web is not relevant
This issue can be mitigated with Webpack config
{
node: {
module: "empty"
}
}
Afterwards, things move along farther, but I needed further mitigations:
import pdfjsLib from "pdfjs-dist/webpack";
This runs pdfjs-dist/webpack.js:27
which is
var PdfjsWorker = require("worker-loader!./build/pdf.worker.js");
Which is attempting to load pdf.worker.js
(which worker-loader
should be packaging) and then tries to instantiate the class:
pdfjs.GlobalWorkerOptions.workerPort = new PdfjsWorker();
The issue I had was that Webpack packaged pdf.worker.js
as an esModule (the default for worker-loader
), so the way it was require’d needs to be unwrapped with the default
property on the imported esModule (said another way, the instantiation would have to be new PdfjsWorker.default()
I was able to mitigate this with the NormalModuleReplacementPlugin
plugin, which is able to re-write the require statement based on a regex match/replace, which is matching the original require string and replacing it with one that sets the worker-loader
option esModule=false
, followed by the absolute path to the pdf.worker.js
file on the local system:
new webpack.NormalModuleReplacementPlugin(
/worker-loader!\.\/build\/pdf\.worker\.js$/,
"worker-loader?esModule=false!" + path.join(__dirname, "../", "node_modules", "pdfjs-dist", "build", "pdf.worker.js")
)
It’s important to match the complete original require string of /worker-loader!\.\/build\/pdf\.worker\.js$/
and not just the pdf.worker.js
part, because you could end up in an infinite replace loop.
You need to fix the replacement string to be a proper path for your project, which would probably be
"worker-loader?esModule=false!" + path.join(__dirname, "node_modules", "pdfjs-dist", "build", "pdf.worker.js")
I have a ../
in my path because this code is being executed inside storybooks .storybook/
folder, so I have go up a directory to get to node_modules/
And with those two changes, everything for PDF.js seems to be working.
And lastly, if you want to ignore the warnings about the missing FetchCompileWasmPlugin
and FetchCompileAsyncWasmPlugin
modules, you can setup the webpack IgnorePlugin
to just ignore these imports, my assumption is they’re WASM based and not actually needed
plugins: [
new webpack.IgnorePlugin({ resourceRegExp: /FetchCompileWasmPlugin$/ }),
new webpack.IgnorePlugin({ resourceRegExp: /FetchCompileAsyncWasmPlugin$/ })
]
I’m guessing there might be some out-of-date mismatch of worker-loader
and the modules in the currently installed Webpack version, but these WASM modules don’t seem to be necessary for our purposes
- Client side JS (e.g. AngularJS) + Django REST Backend: Deploy onto single PaaS?
- Django: What is the most ideal place to store project-specific middleware?
- Format of timesince filter
- Django aggregation: sum then average
1👍
If you’re fine with using a cdn then use this
import pdfJS from 'pdfjs-dist/build/pdf.js';
pdfJS.GlobalWorkerOptions.workerSrc = 'https://cdnjs.cloudflare.com/ajax/libs/pdf.js/2.4.456/pdf.worker.js';
Make sure to import minified versions on production
import pdfJS from 'pdfjs-dist/build/pdf.min.js';
pdfJS.GlobalWorkerOptions.workerSrc = 'https://cdnjs.cloudflare.com/ajax/libs/pdf.js/2.4.456/pdf.worker.min.js';
Or you can just use minified versions all the time
- Yuglify compressor can't find binary from package installed through npm
- Keras error on predict
- Django: How to change a field widget in a Inline Formset
1👍
It worked with:
var pdflib = require('pdfjs-dist/build/pdf.js');
import pdfjsWorker from 'pdfjs-dist/build/pdf.worker.js';
pdflib.GlobalWorkerOptions.workerPort = new pdfjsWorker();
- Django Rest Framework use DjangoModelPermissions on ListAPIView
- PIL – libjpeg.so.8: cannot open shared object file: No such file or directory
1👍
if webpack is failing because of optional chaining consider pointing to the legacy version which omits modern syntax.
import * as pdfjs from 'pdfjs-dist/legacy/build/pdf.js'
instead of
import * as pdfjs from 'pdfjs-dist'
-1👍
Go to
node_modules/pdfsj-dist/package.json
In
"peerDependencies": {
"worker-loader": "^3.0.8" -> Version
},
Install version
npm i worker-loader@3.0.8
- Get params validation on viewsets.ModelViewSet
- Is it correct to modify old migration files in Django?