Load Nodejs Module into A Web Worker

荒凉一梦 提交于 2019-12-19 02:54:14

问题


I'm intending to use web worker inside my Node.js application for some concurrent tasks. However since the 'webworker-threads' module follows the implementation of HTML5 web worker, requiring Nodejs modules like require("fs") inside web worker does not work. importScripts() can load js files but I would like a functionality inside the web worker so that I can require npm-installed modules. Is there a workaround for that?


回答1:


author of webworker-threads here. Thank you for using the module!

There is a default native_fs_ object with the readFileSync you can use to read files.

Beyond that, I've mostly relied on onejs to compile all required modules in package.json into a single JS file for importScripts to use, just like one would do when deploying to a client-side web worker environment. (There are also many alternatives to onejs -- browserify, etc.)

Hope this helps!



来源:https://stackoverflow.com/questions/14107521/load-nodejs-module-into-a-web-worker

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!