electron-vue的ReferenceError:process is not defined解决

2020-07-12 17:45:36

electron-vue在Windows下面报process is not defined错误

使用electron-vue开发的时候报了如下错误

# 运行环境

  • node v12.16.3
  • npm 6.14.4
  • webpack v4.15.1
  • vue v2.5.16
  • vue-electron v1.0.6
  • electron v4.2.5
  • 系统 windows Server 2012 R2

# 解决方法

修改项目文件
.electron-vue目录下
- webpack.renderer.config.js
- webpack.web.config.js

在两个文件的new HtmlWebpackPlugin方法内(template及minify参数之间)追加代码,最终如下所示


// webpack.web.config.js
new HtmlWebpackPlugin({
    filename: 'index.html',
    template: path.resolve(__dirname, '../src/index.ejs'),
    // ----fix process is not defined----
    templateParameters(compilation, assets, options) {
        return {
            compilation: compilation,
            webpack: compilation.getStats().toJson(),
            webpackConfig: compilation.options,
            htmlWebpackPlugin: {
            files: assets,
            options: options
            },
            process,
        };
    },
    // ----fix process is not defined----
    minify: {
    collapseWhitespace: true,
    removeAttributeQuotes: true,
    removeComments: true
    },
    nodeModules: false
}),

// webpack.renderer.config.js
new HtmlWebpackPlugin({
    filename: 'index.html',
    template: path.resolve(__dirname, '../src/index.ejs'),
    // ----fix process is not defined----
    templateParameters(compilation, assets, options) {
        return {
            compilation: compilation,
            webpack: compilation.getStats().toJson(),
            webpackConfig: compilation.options,
            htmlWebpackPlugin: {
            files: assets,
            options: options
            },
            process,
        };
    },
    // ----fix process is not defined----
    minify: {
    collapseWhitespace: true,
    removeAttributeQuotes: true,
    removeComments: true
    },
    nodeModules: process.env.NODE_ENV !== 'production'
    ? path.resolve(__dirname, '../node_modules')
    : false
}),

# 测试

npm run dev就可以了

# 参考资料

issue:https://github.com/SimulatedGREG/electron-vue/issues/871 (opens new window)

本文链接:
版权声明:本博客所有文章除特别声明外,均采用 CC BY-NC-ND 3.0 许可协议。可自由转载、引用,但需署名作者且注明文章出处。如转载至微信公众号,请在文末添加作者公众号二维码。

扫描下方二维码阅读当前文章

浏览器、微信扫码

评 论:

好文推荐
每天进步一点点~