In an electron app just importing libp2p in main.ts causes “self is not defined”

In in electron app just importing libp2p in main.ts :

const Libp2p = require('libp2p');

causes this error:

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/**/*"]
}

devDependencies :

"@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"

dependencies:

"libp2p": "^0.30.4",
"libp2p-kad-dht": "^0.20.6",
  • node version: v14.5.0
  • O.S. : Ubuntu 18.04.4 Desktop

I discovered that this happens also when importing another libp2p library:

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

How to solve the problem?

This is resolved in In an electron app just importing libp2p in main.ts causes "self is not defined" · Issue #873 · libp2p/js-libp2p · GitHub

Let me know if there is any outstanding problem