Friday 6 September 2019

How to import a node module inside an angular web worker?

I try to import a node module inside an Angular 8 web worker, but get an compile error 'Cannot find module'. Anyone know how to solve this?

I created a new worker inside my electron project with ng generate web-worker app, like described in the above mentioned ng documentation.

All works fine until i add some import like path or fs-extra e.g.:

/// <reference lib="webworker" />    
import * as path from 'path';

addEventListener('message', ({ data }) => {    
  console.log(path.resolve('/'))
  const response = `worker response to ${data}`;
  postMessage(response);
});

This import works fine in any other ts component but inside the web worker i get a compile error with this message e.g.

Error: app/app.worker.ts:3:23 - error TS2307: Cannot find module 'path'.

How can i fix this? Maybe i need some additional parameter in the generated tsconfig.worker.json?

To reproduce the error, run:

$ git clone https://github.com/hoefling/stackoverflow-57774039
$ cd stackoverflow-57774039
$ yarn build

Or check out the project's build log on Travis.

Note:

1) I only found this as a similar problem, but the answer handles only custom modules.

2) I tested the same import with a minimal electron seed which uses web workers and it worked, but this example uses plain java script without angular.



from How to import a node module inside an angular web worker?

No comments:

Post a Comment