webtorrent / webtorrent

⚡️ Streaming torrent client for the web
https://webtorrent.io
MIT License
29.54k stars 2.71k forks source link

Absolute path in electron #2871

Open Prof1-web opened 6 days ago

Prof1-web commented 6 days ago

What version of this package are you using? Latest

What operating system, Node.js, and npm version? Node 20 with yarn, tried mac os and windows

What happened? I have an app with electron forge, webtorrent and webpack. I created it using 2 commands:

npm init electron-app@latest my-app -- --template=webpack
yarn add webtorrent

Add import to main.js

import WebTorrent from "webtorrent";

And:

yarn package

After this my application only works on my computer because after the build there is an absolute path in the application code:

(0,n.createRequire)("file:///Users/prof1/projects/pw_castle/launcher_test/node_modules/node-datachannel/lib/index.js")("../build/Release/node_datachannel.node")

I don't understand why this is happening. If I remove the webtorrent import from my main.js, the absolute path disappears from the build. I know that webtorrent requires additional configuration for webpack. But it seems this is only required when used in a browser? Problem is only with webtorrent, other libs work perferctly. I tried different versions of webtorrent. It didn't help me.

My webpack.main.config.js (default for electron forge):

module.exports = {
  entry: './src/main.js',
  module: {
    rules: require('./webpack.rules'),
  },
};

webpack.rules.js (default for electron forge)

module.exports = [
  {
    test: /native_modules[/\\].+\.node$/,
    use: 'node-loader',
  },
  {
    test: /[/\\]node_modules[/\\].+\.(m?js|node)$/,
    parser: { amd: false },
    use: {
      loader: '@vercel/webpack-asset-relocator-loader',
      options: {
        outputAssetBase: 'native_modules',
      },
    },
  },
];

And forge.config.js (default for electron forge):

const { FusesPlugin } = require('@electron-forge/plugin-fuses');
const { FuseV1Options, FuseVersion } = require('@electron/fuses');

module.exports = {
  packagerConfig: {
    asar: true,
  },
  rebuildConfig: {},
  makers: [
    {
      name: '@electron-forge/maker-squirrel',
      config: {},
    },
    {
      name: '@electron-forge/maker-zip',
      platforms: ['darwin'],
    },
    {
      name: '@electron-forge/maker-deb',
      config: {},
    },
    {
      name: '@electron-forge/maker-rpm',
      config: {},
    },
  ],
  plugins: [
    {
      name: '@electron-forge/plugin-auto-unpack-natives',
      config: {},
    },
    {
      name: '@electron-forge/plugin-webpack',
      config: {
        mainConfig: './webpack.main.config.js',
        renderer: {
          config: './webpack.renderer.config.js',
          entryPoints: [
            {
              html: './src/index.html',
              js: './src/renderer.js',
              name: 'main_window',
              preload: {
                js: './src/preload.js',
              },
            },
          ],
        },
      },
    },
    new FusesPlugin({
      version: FuseVersion.V1,
      [FuseV1Options.RunAsNode]: false,
      [FuseV1Options.EnableCookieEncryption]: true,
      [FuseV1Options.EnableNodeOptionsEnvironmentVariable]: false,
      [FuseV1Options.EnableNodeCliInspectArguments]: false,
      [FuseV1Options.EnableEmbeddedAsarIntegrityValidation]: true,
      [FuseV1Options.OnlyLoadAppFromAsar]: true,
    }),
  ],
};
Prof1-web commented 4 days ago

It's actual only for version > 2. I tried 1.9.7 and it works perfectly. This problem is not only with electron. This problem occurs even if you do not use any other libraries except webtorrent. Since version 2, no package builder can work with webtorrent when building for node.js. I tried Webpack, browserify, esbuild, Vite, Percel. The problem is not in the configuration files. Any other libraries are built perfectly by any of the above builders. Please fix this. Or give the ability to use require so that you can skip the build step.

ThaUnknown commented 4 days ago

this is strictly an issue with your build config, not webtorrent, I've used webtorrent on multiple electron projects, and a few other electron projects also use webtorrent without issue, you could always exclude node data channel if you want to patch this rapidly

Prof1-web commented 4 days ago

this is strictly an issue with your build config, not webtorrent, I've used webtorrent on multiple electron projects, and a few other electron projects also use webtorrent without issue, you could always exclude node data channel if you want to patch this rapidly

Can you give me config example for node.js? (Not browser)

ThaUnknown commented 4 days ago

node index.js

node requires no custom module resolution

Prof1-web commented 4 days ago

node index.js

node requires no custom module resolution

But you can't use require () with webtorrent without builders