Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 如何在create react应用程序的web worker中导入已安装的节点模块?_Reactjs_Webpack_Node Modules_Create React App_Web Worker - Fatal编程技术网

Reactjs 如何在create react应用程序的web worker中导入已安装的节点模块?

Reactjs 如何在create react应用程序的web worker中导入已安装的节点模块?,reactjs,webpack,node-modules,create-react-app,web-worker,Reactjs,Webpack,Node Modules,Create React App,Web Worker,我使用createreact应用程序创建了一个简单的React应用程序。 我在public文件夹中添加了这个简单的worker.js: importScripts("fuse.js"); const searchHandler = () => { console.log(this.location.pathname); return "Imported Module!"; }; onmessage = (e) => { cons

我使用createreact应用程序创建了一个简单的React应用程序。 我在
public
文件夹中添加了这个简单的
worker.js

importScripts("fuse.js");

const searchHandler = () => {
  console.log(this.location.pathname);
  return "Imported Module!";
};

onmessage = (e) => {
  const startTime = new Date().getTime();
  const workerResults = searchHandler();
  postMessage({
    workerResults,
    time: new Date().getTime() - startTime
  });
};
在我的
App.js
中,我调用
worker.js

import React from "react";
import ReactDOM from "react-dom";

function App() {
  const worker = new window.Worker(process.env.PUBLIC_URL + "/worker.js");
  worker.postMessage({});
  worker.onmessage = (e) => {
    const { workerResults, time } = e.data;
    console.log({ workerResults, time });
  };
  return (
    <div>The App!
    </div>
  );
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
注:

  • fuse.js
    已安装,我可以成功导入并使用它 直接在
    App.js
  • 我已经读过了 但是没有找到答案

  • 下面的错误消息说明一切正常。如果您在浏览器中打开以下地址
    http://localhost:3000/fuse.js
    ,它会给您一个找不到的错误。因此webWorker无法导入脚本。
    worker.js:1 Uncaught DOMException: Failed to execute 'importScripts' on 'WorkerGlobalScope': The script at 'http://localhost:3000/fuse.js' failed to load.
        at http://localhost:3000/worker.js:1:1