0

在电子应用程序中,只需在 main.ts 中导入 libp2p :

const Libp2p = require('libp2p');

导致此错误:

A JavaScript error occurred in the main process
Uncaught Exception:
ReferenceError: self is not defined
    at Object../node_modules/multihashing-async/src/sha.browser.js (/home/marco/webMatters/electronMatters/OnlyLibp2p
/.webpack/main/index.js:34203:16)
    at __webpack_require__ (/home/marco/webMatters/electronMatters/OnlyLibp2p/.webpack/main/index.js:21:30)
    at Object../node_modules/libp2p-crypto/src/keys/rsa-class.js (/home/marco/webMatters/electronMatters/OnlyLibp2p
/.webpack/main/index.js:19523:13)

tsconfig.json :

{
  "compilerOptions": {
    "jsx": "react",
    "allowJs": true,
    "target": "ES6",
    "module": "ESNext",
    "skipLibCheck": true,
    "esModuleInterop": true,
    "noImplicitAny": false,
    "sourceMap": true,
    "baseUrl": ".",
    "outDir": "dist",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "paths": {
      "*": ["node_modules/*"],
      "@app/*": ["./src/app/*"],
      "@static/*": ["./src/static/*"],
      "@src/*": ["./src/*"]
    }
  },
  "include": ["src/**/*"]
}

开发依赖:

"@electron-forge/cli": "6.0.0-beta.53",
"@electron-forge/maker-deb": "6.0.0-beta.53",
"@electron-forge/maker-rpm": "6.0.0-beta.53",
"@electron-forge/maker-squirrel": "6.0.0-beta.53",
"@electron-forge/maker-zip": "6.0.0-beta.53",
"@electron-forge/plugin-webpack": "6.0.0-beta.53",
"electron": "^11.2.1",
"typescript": "^4.0.2",
"webpack": "4"

依赖项:

"libp2p": "^0.30.4",
"libp2p-kad-dht": "^0.20.6",
  • 节点版本:v14.5.0
  • 操作系统:Ubuntu 18.04.4 桌面

我发现在导入另一个 libp2p 库时也会发生这种情况:

//const Libp2p = require('libp2p');
const KadDHT = require('libp2p-kad-dht');

如何解决问题?

4

1 回答 1

0

在渲染器进程中使用 import 而不是 require 可以解决问题:

import Libp2p from 'libp2p';
import KadDHT from 'libp2p-kad-dht';
于 2021-01-28T09:57:46.880 回答