I have an electron app running angular 8+. I'm calling a function imported from node_modules
. If I call this function directly in my component, it works fine. If I try to call the same function in a web worker, it fails the build.
// component file
const worker = new Worker('./dead-link-checker.worker', { type: 'module' });
worker.onmessage = ({ data }) => {
console.log(`page got message: ${data}`);
};
worker.postMessage({
domainName: this.domainName,
links: this.linksService.links
});
// webworker file
import { deadLinkChecker } from 'dead-link-checker';
import { tester } from './test';
addEventListener('message', async ({ data }) => {
console.log('in the workerer', data);
// Breaks everything
await deadLinkChecker(data.domainName, undefined, data.links);
// Works fine
tester();
postMessage('link checking complete', undefined);
});
The error:
ERROR in ./src/app/dead-link-checker.worker.ts (./node_modules/worker-plugin/dist/loader.js!./src/app/dead-link-checker.worker.ts)
Module build failed (from ./node_modules/worker-plugin/dist/loader.js):
ModuleNotFoundError: Module not found: Error: Can't resolve 'crypto' in 'C:\js_scripts\jordan-does-link-checking-electron\node_modules\aws-sign2'
at C:\js_scripts\jordan-does-link-checking-electron\node_modules\webpack\lib\Compilation.js:823:10
at C:\js_scripts\jordan-does-link-checking-electron\node_modules\webpack\lib\NormalModuleFactory.js:397:22
at C:\js_scripts\jordan-does-link-checking-electron\node_modules\webpack\lib\NormalModuleFactory.js:130:21
at C:\js_scripts\jordan-does-link-checking-electron\node_modules\webpack\lib\NormalModuleFactory.js:224:22
at C:\js_scripts\jordan-does-link-checking-electron\node_modules\neo-async\async.js:2830:7
at C:\js_scripts\jordan-does-link-checking-electron\node_modules\neo-async\async.js:6877:13
at C:\js_scripts\jordan-does-link-checking-electron\node_modules\webpack\lib\NormalModuleFactory.js:214:25
at C:\js_scripts\jordan-does-link-checking-electron\node_modules\enhanced-resolve\lib\Resolver.js:184:12
at C:\js_scripts\jordan-does-link-checking-electron\node_modules\enhanced-resolve\lib\Resolver.js:238:5
at eval (eval at create (C:\js_scripts\jordan-does-link-checking-electron\node_modules\tapable\lib\HookCodeFactory.js:33:10), <anonymous>:15:1)
at C:\js_scripts\jordan-does-link-checking-electron\node_modules\enhanced-resolve\lib\UnsafeCachePlugin.js:37:5
at C:\js_scripts\jordan-does-link-checking-electron\node_modules\enhanced-resolve\lib\Resolver.js:238:5
at eval (eval at create (C:\js_scripts\jordan-does-link-checking-electron\node_modules\tapable\lib\HookCodeFactory.js:33:10), <anonymous>:15:1)
at C:\js_scripts\jordan-does-link-checking-electron\node_modules\enhanced-resolve\lib\Resolver.js:238:5
at eval (eval at create (C:\js_scripts\jordan-does-link-checking-electron\node_modules\tapable\lib\HookCodeFactory.js:33:10), <anonymous>:27:1)
at C:\js_scripts\jordan-does-link-checking-electron\node_modules\enhanced-resolve\lib\DescriptionFilePlugin.js:42:38
I've had this kind of error before using nodejs scripts in angular and I have used solutions like this to resolve it. This doesn't seem to be the same issue here because the imported deadLinkChecker
function works fine if I use it outside of the web worker.