在 angular/electron 应用程序中使用带有 socket.io 的 nodejs 脚本

Using nodejs script with socket.io inside an angular/electron application

我正在构建 electron/angular 应用程序 - 它工作正常; 我有另一个打开 socket.io 服务器的 nodejs 脚本; (我正在使用 typescript + webpack 在单个 bundle js 文件中创建所有文件)。 我试图将此脚本包含在 anguler/electron 中。但它看起来像 socket.io(或其他一些包含在脚本中的库)依赖于 nodejs 内置包并且它不是 pos 可以在 angular 应用程序中使用它。这对我来说很有意义,因为你不能直接在浏览器上启动套接字服务器——但它是一个桌面 eletron 应用程序。

当我尝试为包构建 angular 应用程序时出现此错误: 路径、os、http、https、加密、tty、zlib、util

Error: Module not found: Error: Can't resolve 'util' in '..\src\app'

BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default.
This is no longer the case. Verify if you need this module and configure a polyfill for it.

If you want to include a polyfill, you need to:
        - add a fallback 'resolve.fallback: { "util": require.resolve("util/") }'      
        - install 'util'
If you don't want to include a polyfill, you can use an empty module like this:        
        resolve.fallback: { "util": false }
nodejs脚本的

webpack.config

const path = require('path');

module.exports = {
  entry: './src/index.ts',
  mode: 'production',
  target: 'node',
  module: {
    rules: [
      {
        test: /\.ts?$/,
        use: 'ts-loader',
        exclude: /node_modules/,
      },
    ],
  },
  resolve: {
    extensions: ['.ts', '.js'],
  },
  output: {
    filename: 'script.min.js',
    path: path.resolve(__dirname, 'dist'),
    libraryTarget: 'umd',
    libraryExport: 'default'
  },
};

我可以不将此脚本包含在 angular 应用程序中,而是包含在 main.js 电子应用程序从哪里开始......但是这样我将无法访问中的内容来自 Angular 里面的脚本;

我在这个逻辑上哪里失败了? 想要的结果是一个 electron/angular 桌面应用程序,我在其中有一个按钮,当单击它时它会启动 socket.io 服务器并提供配置。

您只需将其添加到您的 webpack 配置中即可解决此问题。我不使用 Angular 但在 React 中我将其添加到 webpack.renderer.config.js 我建议阅读有关在 Electron 中创建辅助 windows 和 运行 所有这些内容。

resolve: {
    // all your other configs
      fallback: {
       "util": false,
      }
    }