【问题标题】:node_modules directory is empty in packaged app when using @electron-forge/plugin-webpack使用@electron-forge/plugin-webpack 时,打包应用程序中的 node_modules 目录为空
【发布时间】:2023-02-26 00:41:22
【问题描述】:

在我使用@electron-forge/plugin-webpack 插件的电子应用程序中。这是我的forge.config.js 文件。

  module.exports = {
    rebuildConfig: {},
    publishers: [
        {
            name: '@electron-forge/publisher-github',
            config: {
                repository: {
                    owner: '...',
                    name: '...'
                },
            }
        }
    ],
    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: {},
        },
    ],
    // comment the block below inorder to copy the node_modules to the output when packaging
    plugins: [
        {
            name: '@electron-forge/plugin-webpack',
            config: {
                mainConfig: './webpack.main.config.js',
                devContentSecurityPolicy: "connect-src 'self' * 'unsafe-eval'",
                renderer: {
                    config: './webpack.renderer.config.js',
                    entryPoints: [
                        {
                            html: './src/public/home.html',
                            js: './src/renderer.js',
                            name: 'main_window',
                            preload: {
                                js: './src/preload.js'
                            },
                        },
                    ],
                },
            },
        },
    ],
};

当我运行npm run package时,out/<app>/resources/app/node_modules目录是空的。但是,如果我删除插件部分,则会正确复制 node_modules。但我不能评论插件部分,因为需要 webpack。

我是否配置了任何错误或遗漏了什么?

【问题讨论】:

    标签: webpack electron electron-forge


    【解决方案1】:

    在这里我有一个答案给你,让我知道这是否有帮助!

    使用@electron-forge/plugin-webpack插件时,预计打包后的应用中node_modules目录为空。这是因为 webpack 将所有必需的模块捆绑到一个文件或几个文件中,然后将其包含在打包的应用程序中。因此,打包后的应用中不需要包含node_modules目录。

    如果你想在打包的应用中包含node_modules目录,你可以在打包时将vforge.config.jsfile. This block will copy thenode_modules`目录中makers部分下面的块取消注释到输出。

    但是不建议在打包后的应用中包含node_modules目录,会增加打包后的大小。此外,由于开发环境和生产环境之间的差异,某些模块可能无法在打包的应用程序中正常工作。

    相反,您应该确保所有必需的模块都包含在 webpack 包中。您可以通过在 package.json 文件中将所需模块指定为 dependencies 或 devDependencies 并配置 webpack 以正确捆绑它们来完成此操作。

    如果您仍然遇到问题,您可以尝试将@electron-forge/plugin-webpack插件更新到最新版本,或者您可以联系插件的支持渠道以获得进一步的帮助。

    【讨论】:

      猜你喜欢
      • 2016-08-26
      • 2021-02-17
      • 2021-08-28
      • 1970-01-01
      • 1970-01-01
      • 2021-01-09
      • 2018-04-02
      • 1970-01-01
      • 2017-08-31
      相关资源
      最近更新 更多