Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot import built-in node modules in worker in electron/webpack build #10

Open
jc-lab opened this issue Jan 6, 2020 · 16 comments
Open
Labels
bug Something isn't working

Comments

@jc-lab
Copy link

jc-lab commented Jan 6, 2020

https://github.com/jc-lab/threads-plugin-problem
See the repository above.

Error when using module as esnext in tsconfig.
If you use commonjs instead of esnext, threads-plugin will not detect Worker.

> npm run build
...
Child WorkerPluginLoader:
     2 assets
    Entrypoint 0 = 0.bundle.worker.js 0.bundle.worker.js.map
     [6] (webpack)/buildin/harmony-module.js 573 bytes {0} [built]
    [15] ./node_modules/ts-loader??ref--4!./src/test.thread.ts + 2 modules 16.7 KiB {0} [built]
         | ./node_modules/ts-loader??ref--4!./src/test.thread.ts 730 bytes [built]
         |     + 2 hidden modules
        + 26 hidden modules

    ERROR in ./src/test.thread.ts (./node_modules/ts-loader??ref--4!./src/test.thread.ts)
    Module not found: Error: Can't resolve 'fs' in 'D:\temp\webpack-worker-test\src'
     @ ./src/test.thread.ts (./node_modules/ts-loader??ref--4!./src/test.thread.ts) 3:0-25 8:22-42

    ERROR in ./src/test.thread.ts (./node_modules/ts-loader??ref--4!./src/test.thread.ts)
    Module not found: Error: Can't resolve 'zlib' in 'D:\temp\webpack-worker-test\src'
     @ ./src/test.thread.ts (./node_modules/ts-loader??ref--4!./src/test.thread.ts) 4:0-29 9:23-41

@andywer andywer changed the title Not working in typescript Not working in typescript when output is CommonJS modules Jan 6, 2020
@andywer
Copy link
Owner

andywer commented Jan 6, 2020

It works with TypeScript, you just need to set module to esnext as you already wrote. It's also documented like that: See here.

The error above is unrelated to threads.js and the threads-plugin as far as I can tell.

Try npm install --save-dev @types/node.

@andywer andywer closed this as completed Jan 6, 2020
@andywer
Copy link
Owner

andywer commented Jan 6, 2020

Feel free to re-open if I missed something. The error above looks very much like something else is fishy, though.

@jc-lab
Copy link
Author

jc-lab commented Jan 6, 2020

@andywer
If you comment out ThreadsPlugin as shown below, no error will occur.
That's why I think it's an issue of threads-plugin.

// plugins: [new ThreadPlugin()],

output:

> npm run build
> webpack --config webpack.config.js -p

Hash: 1fb9dddabef6f1be9dc0
Version: webpack 4.41.5
Time: 1404ms
Built at: 2020-01-06 23:04:43
                          Asset       Size  Chunks                   Chunk Names
          ..\dist-ts\index.d.ts   47 bytes          [emitted]
      ..\dist-ts\index.d.ts.map  104 bytes          [emitted]
    ..\dist-ts\test.thread.d.ts   53 bytes          [emitted]
..\dist-ts\test.thread.d.ts.map  116 bytes          [emitted]
                      bundle.js   7.37 KiB       0  [emitted]        main
                  bundle.js.map   4.43 KiB       0  [emitted] [dev]  main
Entrypoint main = bundle.js bundle.js.map
[0] external "threads" 42 bytes {0} [built]
[1] ./src/index.ts 3.26 KiB {0} [built]

@andywer
Copy link
Owner

andywer commented Jan 6, 2020

Well, if you out-comment the threads-plugin, then webpack won't even know that it should process the worker script and its dependencies, so it won't crash as the worker's code isn't touched by webpack at all 😉

Have you installed the @types/node package?

@jc-lab
Copy link
Author

jc-lab commented Jan 6, 2020

https://github.com/jc-lab/threads-plugin-problem/blob/14b7cdaf46335667902538ecd20009ce2e6812b3/package.json#L9

As shown above, @types/node is installed.
The same code in test.worker.ts works fine with index.ts.

@andywer
Copy link
Owner

andywer commented Jan 6, 2020

Wow, that seems like a weird issue and requires more time to investigate then.

@andywer andywer reopened this Jan 6, 2020
@andywer andywer changed the title Not working in typescript when output is CommonJS modules Cannot import built-in node modules in worker in electron/webpack build Jan 6, 2020
@andywer andywer added the bug Something isn't working label Jan 6, 2020
@jc-lab
Copy link
Author

jc-lab commented Jan 6, 2020

@andywer Please review. Thank you.

@andywer
Copy link
Owner

andywer commented Mar 26, 2020

Can you please check if it's fixed in the latest version? #16 might have fixed it.

@raz-sinay
Copy link

raz-sinay commented Apr 5, 2020

I have encountered this issue.
solved it by doing:

// webpack.main.js
const NodeTargetPlugin = require('webpack/lib/node/NodeTargetPlugin');
const ThreadsPlugin = require('threads-plugin');
module.exports = function(config) {
  // NodeTargetPlugin must be used in order to use nodejs built-in API in workers.
  config.plugins.unshift(
    new ThreadsPlugin({
      plugins: [new NodeTargetPlugin()]
    })
  );
  return config;
};

this works for both main thread and renderer threads

@andywer
Copy link
Owner

andywer commented Apr 5, 2020

@raz-sinay Cool, thanks!

@jc-lab Can you try and confirm that it works? If so, let's add a target option to the threads-plugin that defaults to the webpack config's main target option and adds the NodeTargetPlugin if the new setting is node.

@pverscha
Copy link

pverscha commented May 11, 2020

@andywer @jc-lab I've just discovered that you need to enable nodeIntegrationInWorker when creating a new BrowserWindow for fs to work. See https://www.electronjs.org/docs/tutorial/multithreading for more information.

@andywer
Copy link
Owner

andywer commented May 12, 2020

@pverscha Sure, but you shouldn't… 😅 One little XSS vulnerability in your webapp code and the attacker can take over your whole system at once ;)

I wouldn't use fs in a BrowserWindow context at all, but multithreading should still be possible out of the box - in the web context via web workers and outside of it using worker threads.

(That little security insight is brought to you by someone building cryptocurrency wallets with Electron for a living, among other things ;) )

@pverscha
Copy link

@andywer Thanks! Didn't think of that.

@arition
Copy link

arition commented Sep 30, 2020

@raz-sinay Cool, thanks!

@jc-lab Can you try and confirm that it works? If so, let's add a target option to the threads-plugin that defaults to the webpack config's main target option and adds the NodeTargetPlugin if the new setting is node.

I met the same problem and the NodeTargetPlugin trick works for me. Maybe you can add this to the documentation?

@andywer
Copy link
Owner

andywer commented Sep 30, 2020

@arition Hmm, are you using the latest version of the threads-plugin? As you can see in the loader code, the behavior that I outlined before has already been implemented.

So if you set the webpack target option to something containing node, it will automatically add the NodeTargetPlugin to the worker compiler.

@arition
Copy link

arition commented Sep 30, 2020

@andywer I think the problem still exists because the target is electron-main in my case. The target name does not contain node.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants