electron-vue icon indicating copy to clipboard operation
electron-vue copied to clipboard

Webpack ReferenceError: process is not defined

Open lgrcia opened this issue 6 years ago • 63 comments

Issue

Fresh electron-vue project gives webpack error: ReferenceError: process is not defined

Capture d’écran 2019-05-05 à 15 58 26 Look like issue #516 solved by #726 for build:web
Reproduction
vue init simulatedgreg/electron-vue test_vue_error
cd test_vue_error
npm install
npm run dev
Screenshot of rendering error
Capture d’écran 2019-05-05 à 15 45 05
Development environment
  • Node version: v12.1.0
  • NPM version: 6.9.0
  • vue-cli version: 2.9.6
  • Vue version: 2.6.10
  • Webpack version: 4.30.0
  • Operating System: OSX 10.14.4

lgrcia avatar May 05 '19 14:05 lgrcia

Solved by removing:

<% if (!process.browser) { %>
    <script>
        if (process.env.NODE_ENV !== 'development') window.__static = require('path').join(__dirname, '/static').replace(/\\/g, '\\\\')
    </script>
<% } %>

from src/index.ejs:16

Might not be the right solution but works as a temporary fix for me. Not sure issue should be closed though!

lgrcia avatar May 06 '19 09:05 lgrcia

I also ran into this issue. Is it possible that your Node version changed? I find that I can replicate this problem consistently by upgrading/downgrading Node, using NVM to switch between various versions. The fix, for me, was to rm -rf node_modules; npm i; npm run dev, after making the version change. Update: I get this error consistently with Node v12.1.0 and cannot get around it by re-installing dependencies.

jeremyfromearth avatar May 07 '19 05:05 jeremyfromearth

Experiencing the same issue on Node 12.2.0. Not sure what causes it, and not entirely convinced the issue is in this repo. Probably a dependency. Removing parts from the template will work for npm run dev, but not so much for npm run build or npm run pack.

My temporary solution was to do modify HtmlWebpackPlugin in .electron-vue/webpack.web.config.js and .electron-vue/webpack.renderer.config.js:

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

wubzz avatar May 08 '19 06:05 wubzz

I refactored my project to use this: https://github.com/nklayman/vue-cli-plugin-electron-builder. Works w/ Node 12, is generally simpler and more well documented.

jeremyfromearth avatar May 08 '19 14:05 jeremyfromearth

The same error appeared after updating nodejs to 12.2.0 (in 11 it was all working). The solution of wubzz solves it.

JBtje avatar May 17 '19 12:05 JBtje

Experiencing the same issue on Node 12.2.0. Not sure what causes it, and not entirely convinced the issue is in this repo. Probably a dependency. Removing parts from the template will work for npm run dev, but not so much for npm run build or npm run pack.

My temporary solution was to do modify HtmlWebpackPlugin in .electron-vue/webpack.web.config.js and .electron-vue/webpack.renderer.config.js:

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

I'm a new boy for javascript, how to do that, detail please

AIMentalModel avatar May 18 '19 16:05 AIMentalModel

@AIMentalModel have you first tried the solution from @LionelGarcia - about removing the section from the index.ejs file? I only mention because it has worked well for me.

link to the comment -> https://github.com/SimulatedGREG/electron-vue/issues/871#issuecomment-489556092

kmaher9 avatar May 21 '19 14:05 kmaher9

I also run into this issue after upgraded version of node to latest. I need v12 to use web workers. I don't think removing following:

<!-- Set `__static` path to static files in production -->
<% if (!process.browser) { %>
  <script>
    if (process.env.NODE_ENV !== 'development') window.__static = require('path').join(__dirname, '/static').replace(/\\/g, '\\\\')
  </script>
<% } %>

is a real solution. I'm not sure how it will behavior when I'll switch from development to production. Any thoughts about that?

I'm using windows 10.


Edit However this is weird because when I dig some more I found out that local NodeJS version doesn't really matter because vue-electron ship its own NodeJS copy.

@LionelGarcia you wrote that you have node v12.1.0 but do you mean your local node or you somehow updated node shipped with electron-vue?

you can check local version by node -v in terminal and you can check your electron-vue node version by opening dev tools within electron window and calling process.versions in console. In my case:

image

and local:

image

d0peCode avatar May 28 '19 03:05 d0peCode

@kmaher9 i think i test it first by that solution, but i have something error for my project. my work need a component for vue, but i take the error ,so i use that component for electron-react. sorry about that.

AIMentalModel avatar May 28 '19 03:05 AIMentalModel

@kmaher9 i think i test it first by that solution, but i have something error for my project. my work need a component for vue, but i take the error ,so i use that component for electron-react. sorry about that.

Cool well I'm glad you found a solution, consider closing this ticket off if your issue is resolved :)

kmaher9 avatar May 30 '19 22:05 kmaher9

Seems to work for me using an older node version, node v11.15.0 works, with node v12.x i get the ReferenceError: process is not defined error

nattvara avatar Jun 01 '19 15:06 nattvara

in electron v5.x. You should addnodeIntegration in main process

mainWindow = new BrowserWindow({
    height: 563,
    useContentSize: true,
    width: 1000,
    title: 'TBFE',
    webPreferences: {
      nodeIntegration: true // add this
    }
  })

And it will resolve this problem.

xtx1130 avatar Jun 03 '19 11:06 xtx1130

add this code solved the problem for me

templateParameters(compilation, assets, options) {
        return {
          compilation: compilation,
          webpack: compilation.getStats().toJson(),
          webpackConfig: compilation.options,
          htmlWebpackPlugin: {
            files: assets,
            options: options
          },
          process,
        };
      }

hiepvq avatar Jun 04 '19 08:06 hiepvq

add this code solved the problem for me

templateParameters(compilation, assets, options) {
        return {
          compilation: compilation,
          webpack: compilation.getStats().toJson(),
          webpackConfig: compilation.options,
          htmlWebpackPlugin: {
            files: assets,
            options: options
          },
          process,
        };
      }

where add this your code ?

tvortsa avatar Jul 27 '19 00:07 tvortsa

add this code solved the problem for me

templateParameters(compilation, assets, options) {
        return {
          compilation: compilation,
          webpack: compilation.getStats().toJson(),
          webpackConfig: compilation.options,
          htmlWebpackPlugin: {
            files: assets,
            options: options
          },
          process,
        };
      }

where add this your code ?

...electron-vue\webpack.renderer.config.js ...electron-vue\webpack.web.config.js

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

hiepvq avatar Jul 27 '19 07:07 hiepvq

i tried and only solution from @wubzz works

hulkyhawk avatar Aug 12 '19 08:08 hulkyhawk

I have the same issue... :( In my case the proposed workaround from @d0peCode worked for me.

λ npm -v
6.10.3
λ yarn -v
1.15.2
λ node -v
v12.8.0

burzum avatar Aug 16 '19 11:08 burzum

i had same issue, I solved by changing node version, i was using node v12.9.1 i changed to v10.16.2 and the issue was solved

ErcilioMarques avatar Aug 29 '19 12:08 ErcilioMarques

I edit webpack.renderer.config.js as below:

new HtmlWebpackPlugin({
      filename: 'index.html',
      template: path.resolve(__dirname, '../src/index.ejs'),
      minify: {
        collapseWhitespace: true,
        removeAttributeQuotes: true,
        removeComments: true
      },
      isBrowser: false,
      isDevelopment: process.env.NODE_ENV !== 'production',
      nodeModules: process.env.NODE_ENV !== 'production'
        ? path.resolve(__dirname, '../node_modules')
        : false
    }),

and then change index.ejs like this:

<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="utf-8">
    <% if (htmlWebpackPlugin.options.nodeModules) { %>
        <!-- Add `node_modules/` to global paths so `require` works properly in development -->
        <script>
          require('module').globalPaths.push('<%= htmlWebpackPlugin.options.nodeModules.replace(/\\/g, '\\\\') %>')
        </script>
    <% } %>
</head>
<body>

<div id="app">
</div>

<!-- Set `__static` path to static files in production -->
<% if (!htmlWebpackPlugin.options.isBrowser && !htmlWebpackPlugin.options.isDevelopment) { %>
    <script>
      window.__static = require('path').join(__dirname, '/static').replace(/\\/g, '\\\\')
    </script>
<% } %>

<!-- webpack builds are automatically injected -->
</body>
</html>

it just drops "process" and works perfectly for me.

I hope this can help you.

yikuo123 avatar Sep 10 '19 07:09 yikuo123

I have the same issue

npm -v 6.10.2

yarn -v 1.7.0

node -v v12.8.1

snowdream avatar Sep 25 '19 10:09 snowdream

For this to have a PR that was merged to master and supposedly fixed the issue, I'm still getting the same error as well.

jdfwarrior avatar Oct 01 '19 04:10 jdfwarrior

I have the same issue. But earlier today, it didn't happen in Windows.

baruchiro avatar Oct 07 '19 16:10 baruchiro

in electron v5.x. You should addnodeIntegration in main process

mainWindow = new BrowserWindow({
    height: 563,
    useContentSize: true,
    width: 1000,
    title: 'TBFE',
    webPreferences: {
      nodeIntegration: true // add this
    }
  })

And it will resolve this problem.

solved

aliraza114 avatar Oct 09 '19 07:10 aliraza114

I edit webpack.renderer.config.js as below:

new HtmlWebpackPlugin({
      filename: 'index.html',
      template: path.resolve(__dirname, '../src/index.ejs'),
      minify: {
        collapseWhitespace: true,
        removeAttributeQuotes: true,
        removeComments: true
      },
      isBrowser: false,
      isDevelopment: process.env.NODE_ENV !== 'production',
      nodeModules: process.env.NODE_ENV !== 'production'
        ? path.resolve(__dirname, '../node_modules')
        : false
    }),

and then change index.ejs like this:

<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="utf-8">
    <% if (htmlWebpackPlugin.options.nodeModules) { %>
        <!-- Add `node_modules/` to global paths so `require` works properly in development -->
        <script>
          require('module').globalPaths.push('<%= htmlWebpackPlugin.options.nodeModules.replace(/\\/g, '\\\\') %>')
        </script>
    <% } %>
</head>
<body>

<div id="app">
</div>

<!-- Set `__static` path to static files in production -->
<% if (!htmlWebpackPlugin.options.isBrowser && !htmlWebpackPlugin.options.isDevelopment) { %>
    <script>
      window.__static = require('path').join(__dirname, '/static').replace(/\\/g, '\\\\')
    </script>
<% } %>

<!-- webpack builds are automatically injected -->
</body>
</html>

it just drops "process" and works perfectly for me.

I hope this can help you.

This worked for me. Thanks --Node version v12.10.0

mbach04 avatar Oct 12 '19 16:10 mbach04

Seems to work for me using an older node version, node v11.15.0 works, with node v12.x i get the ReferenceError: process is not defined error

True! I spend 2 days deleting code, add code and in the end it was my node version! So, WITH node V12 really don't work! Use an older and there you have it!

ghost avatar Oct 23 '19 14:10 ghost

I edit webpack.renderer.config.js as below:

new HtmlWebpackPlugin({
      filename: 'index.html',
      template: path.resolve(__dirname, '../src/index.ejs'),
      minify: {
        collapseWhitespace: true,
        removeAttributeQuotes: true,
        removeComments: true
      },
      isBrowser: false,
      isDevelopment: process.env.NODE_ENV !== 'production',
      nodeModules: process.env.NODE_ENV !== 'production'
        ? path.resolve(__dirname, '../node_modules')
        : false
    }),

and then change index.ejs like this:

<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="utf-8">
    <% if (htmlWebpackPlugin.options.nodeModules) { %>
        <!-- Add `node_modules/` to global paths so `require` works properly in development -->
        <script>
          require('module').globalPaths.push('<%= htmlWebpackPlugin.options.nodeModules.replace(/\\/g, '\\\\') %>')
        </script>
    <% } %>
</head>
<body>

<div id="app">
</div>

<!-- Set `__static` path to static files in production -->
<% if (!htmlWebpackPlugin.options.isBrowser && !htmlWebpackPlugin.options.isDevelopment) { %>
    <script>
      window.__static = require('path').join(__dirname, '/static').replace(/\\/g, '\\\\')
    </script>
<% } %>

<!-- webpack builds are automatically injected -->
</body>
</html>

it just drops "process" and works perfectly for me.

I hope this can help you.

this works fine. thanks buddy

encryptedtouhid avatar Nov 14 '19 11:11 encryptedtouhid

I edit webpack.renderer.config.js as below:

new HtmlWebpackPlugin({
      filename: 'index.html',
      template: path.resolve(__dirname, '../src/index.ejs'),
      minify: {
        collapseWhitespace: true,
        removeAttributeQuotes: true,
        removeComments: true
      },
      isBrowser: false,
      isDevelopment: process.env.NODE_ENV !== 'production',
      nodeModules: process.env.NODE_ENV !== 'production'
        ? path.resolve(__dirname, '../node_modules')
        : false
    }),

and then change index.ejs like this:

<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="utf-8">
    <% if (htmlWebpackPlugin.options.nodeModules) { %>
        <!-- Add `node_modules/` to global paths so `require` works properly in development -->
        <script>
          require('module').globalPaths.push('<%= htmlWebpackPlugin.options.nodeModules.replace(/\\/g, '\\\\') %>')
        </script>
    <% } %>
</head>
<body>

<div id="app">
</div>

<!-- Set `__static` path to static files in production -->
<% if (!htmlWebpackPlugin.options.isBrowser && !htmlWebpackPlugin.options.isDevelopment) { %>
    <script>
      window.__static = require('path').join(__dirname, '/static').replace(/\\/g, '\\\\')
    </script>
<% } %>

<!-- webpack builds are automatically injected -->
</body>
</html>

it just drops "process" and works perfectly for me.

I hope this can help you.

This works.

Many thanks!

reezooh avatar Nov 29 '19 17:11 reezooh

All solution of this issues works for me. But I still not really understand why.

My node version is v12.13.1.

I am new guy for js and node

lemtasev avatar Dec 06 '19 02:12 lemtasev

https://github.com/nodejs/node/pull/26882

Pretty much in node v12+ you need to explicitly import the process module.

Only change you need to make is below is update process to require('process'). I Can submit a PR if needed. No changes to your webpack config or anything is needed.

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>vest</title>
    <% if (htmlWebpackPlugin.options.nodeModules) { %>
      <!-- Add `node_modules/` to global paths so `require` works properly in development -->
      <script>
        require('module').globalPaths.push('<%= htmlWebpackPlugin.options.nodeModules.replace(/\\/g, '\\\\') %>')
      </script>
    <% } %>
  </head>
  <body>
    <div id="app"></div>
    <!-- Set `__static` path to static files in production -->
    <%  if (!require('process').browser) { %>
      <script>
        if (process.env.NODE_ENV !== 'development') window.__static = require('path').join(__dirname, '/static').replace(/\\/g, '\\\\')
      </script>
    <% } %>

    <!-- webpack builds are automatically injected -->
  </body>
</html>

ChrisKader avatar Dec 10 '19 23:12 ChrisKader

@ChrisKader 你的时非常正确的解决方案,希望大家采用他的解决方案。——node12.8的工作者的一句忠告。——i am from china

feiquan666 avatar Dec 13 '19 03:12 feiquan666

nodejs/node#26882

Pretty much in node v12+ you need to explicitly import the process module.

Only change you need to make is below is update process to require('process'). I Can submit a PR if needed. No changes to your webpack config or anything is needed.

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>vest</title>
    <% if (htmlWebpackPlugin.options.nodeModules) { %>
      <!-- Add `node_modules/` to global paths so `require` works properly in development -->
      <script>
        require('module').globalPaths.push('<%= htmlWebpackPlugin.options.nodeModules.replace(/\\/g, '\\\\') %>')
      </script>
    <% } %>
  </head>
  <body>
    <div id="app"></div>
    <!-- Set `__static` path to static files in production -->
    <%  if (!require('process').browser) { %>
      <script>
        if (process.env.NODE_ENV !== 'development') window.__static = require('path').join(__dirname, '/static').replace(/\\/g, '\\\\')
      </script>
    <% } %>

    <!-- webpack builds are automatically injected -->
  </body>
</html>

Thank you!

cdwmhcc avatar Dec 14 '19 00:12 cdwmhcc

Method 1:
require('process').browser

Method 2:
templateParameters(compilation, assets, options) {
        return {
          compilation: compilation,
          webpack: compilation.getStats().toJson(),
          webpackConfig: compilation.options,
          htmlWebpackPlugin: {
            files: assets,
            options: options
          },
          process,
        };

Either way, after being packaged as a web or app, the menu will not display correctly. But it can be displayed in the dev environment.

What the hell? test

mmm8955405 avatar Dec 15 '19 08:12 mmm8955405

nodejs/node#26882

Pretty much in node v12+ you need to explicitly import the process module.

Only change you need to make is below is update process to require('process'). I Can submit a PR if needed. No changes to your webpack config or anything is needed.

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>vest</title>
    <% if (htmlWebpackPlugin.options.nodeModules) { %>
      <!-- Add `node_modules/` to global paths so `require` works properly in development -->
      <script>
        require('module').globalPaths.push('<%= htmlWebpackPlugin.options.nodeModules.replace(/\\/g, '\\\\') %>')
      </script>
    <% } %>
  </head>
  <body>
    <div id="app"></div>
    <!-- Set `__static` path to static files in production -->
    <%  if (!require('process').browser) { %>
      <script>
        if (process.env.NODE_ENV !== 'development') window.__static = require('path').join(__dirname, '/static').replace(/\\/g, '\\\\')
      </script>
    <% } %>

    <!-- webpack builds are automatically injected -->
  </body>
</html>

it work well, thanks~

HiZhaoYun avatar Dec 26 '19 02:12 HiZhaoYun

I refactored my project to use this: https://github.com/nklayman/vue-cli-plugin-electron-builder. Works w/ Node 12, is generally simpler and more well documented.

Cannot agree more, this works better, and the vue-cli3/4 supported.

hyjiacan avatar Dec 27 '19 01:12 hyjiacan

I edit webpack.renderer.config.js as below:

new HtmlWebpackPlugin({
      filename: 'index.html',
      template: path.resolve(__dirname, '../src/index.ejs'),
      minify: {
        collapseWhitespace: true,
        removeAttributeQuotes: true,
        removeComments: true
      },
      isBrowser: false,
      isDevelopment: process.env.NODE_ENV !== 'production',
      nodeModules: process.env.NODE_ENV !== 'production'
        ? path.resolve(__dirname, '../node_modules')
        : false
    }),

and then change index.ejs like this:

<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="utf-8">
    <% if (htmlWebpackPlugin.options.nodeModules) { %>
        <!-- Add `node_modules/` to global paths so `require` works properly in development -->
        <script>
          require('module').globalPaths.push('<%= htmlWebpackPlugin.options.nodeModules.replace(/\\/g, '\\\\') %>')
        </script>
    <% } %>
</head>
<body>

<div id="app">
</div>

<!-- Set `__static` path to static files in production -->
<% if (!htmlWebpackPlugin.options.isBrowser && !htmlWebpackPlugin.options.isDevelopment) { %>
    <script>
      window.__static = require('path').join(__dirname, '/static').replace(/\\/g, '\\\\')
    </script>
<% } %>

<!-- webpack builds are automatically injected -->
</body>
</html>

it just drops "process" and works perfectly for me.

I hope this can help you.

thank you, my node version is 12.14.1, this works for me.

RyanChent avatar Jan 15 '20 11:01 RyanChent

did anybody explain what is error mean?

Taalaybek avatar Jan 30 '20 21:01 Taalaybek

Is the temporary fix backwards compatible with Node v10.x ? Thanks

KaKi87 avatar Feb 03 '20 17:02 KaKi87

This problem is really annoying, do we get an official fix in order to use latest version of Node? Would be nice, since this post is almost one year old, otherwise fuck this package and i'm going with other solutions...

blackw212 avatar Feb 08 '20 17:02 blackw212

@blackw212 Electron + Parcel works 99% fine 😉

KaKi87 avatar Feb 08 '20 17:02 KaKi87

I can confirm @ChrisKader solution fix this error.

panovr avatar Feb 11 '20 10:02 panovr

When you need to re-configure half of the project to just run hello world, you know that you should not even go near that plugin.. I recommend using https://github.com/nklayman/vue-cli-plugin-electron-builder just imported to my premade project and BAM everything works!

bhaskar-nair2 avatar Mar 24 '20 12:03 bhaskar-nair2

Any node version below 12 seems to work. I had node 13, and got this error. Used NVM to attempt with 10.19, and it works just fine.

So, version compability issue?

PatricNox avatar Apr 07 '20 13:04 PatricNox

Seems to also be general for Electron and not specifically this project.

PatricNox avatar Apr 07 '20 13:04 PatricNox

May as well let this thing die. It doesn’t seem like it’s going to be supported that much anymore. Use the vue cli and plugins and you can easily accomplish all the stuff this template does, and it works.

jdfwarrior avatar Apr 07 '20 14:04 jdfwarrior

as others have pointed out, @ChrisKader's solution works perfectly 👍

in src/index.ejs, you need to change<% if (!process.browser) { %> to <% if (!require('process').browser) { %> and you're in business!

modenero avatar Apr 11 '20 15:04 modenero

@AIMentalModel您是否首先尝试过@LionelGarcia的解决方案-关于从index.ejs文件中删除该部分?我只提到它,因为它对我来说效果很好。

链接到评论-> #871(评论)

Solved by removing:

<% if (!process.browser) { %>
    <script>
        if (process.env.NODE_ENV !== 'development') window.__static = require('path').join(__dirname, '/static').replace(/\\/g, '\\\\')
    </script>
<% } %>

from src/index.ejs:16

Might not be the right solution but works as a temporary fix for me. Not sure issue should be closed though!

solved my problem suscces! but ,What causes this problem?

PachVerb avatar Apr 15 '20 07:04 PachVerb

Experiencing the same issue on Node 12.2.0. Not sure what causes it, and not entirely convinced the issue is in this repo. Probably a dependency. Removing parts from the template will work for npm run dev, but not so much for npm run build or npm run pack.

My temporary solution was to do modify HtmlWebpackPlugin in .electron-vue/webpack.web.config.js and .electron-vue/webpack.renderer.config.js:

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

new questions is module.js:543 Uncaught Error: Cannot find module 'axios' , what happen

PachVerb avatar Apr 15 '20 07:04 PachVerb

nodejs/node#26882

Pretty much in node v12+ you need to explicitly import the process module.

Only change you need to make is below is update process to require('process'). I Can submit a PR if needed. No changes to your webpack config or anything is needed.

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>vest</title>
    <% if (htmlWebpackPlugin.options.nodeModules) { %>
      <!-- Add `node_modules/` to global paths so `require` works properly in development -->
      <script>
        require('module').globalPaths.push('<%= htmlWebpackPlugin.options.nodeModules.replace(/\\/g, '\\\\') %>')
      </script>
    <% } %>
  </head>
  <body>
    <div id="app"></div>
    <!-- Set `__static` path to static files in production -->
    <%  if (!require('process').browser) { %>
      <script>
        if (process.env.NODE_ENV !== 'development') window.__static = require('path').join(__dirname, '/static').replace(/\\/g, '\\\\')
      </script>
    <% } %>

    <!-- webpack builds are automatically injected -->
  </body>
</html>

very good :)

PachVerb avatar Apr 15 '20 07:04 PachVerb

Uncaught Error: Cannot find module 'axios'

yanlee26 avatar Apr 22 '20 02:04 yanlee26

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

Uncaught Error: Cannot find module 'axios' image

kingangelAOA avatar Jun 11 '20 10:06 kingangelAOA

@ChrisKader 你的时非常正确的解决方案,希望大家采用他的解决方案。——node12.8的工作者的一句忠告。——i am from china

独秀同志,赶快落座,你是对的

wentianl20 avatar Jul 05 '20 03:07 wentianl20

Experiencing the same issue on Node 12.2.0. Not sure what causes it, and not entirely convinced the issue is in this repo. Probably a dependency. Removing parts from the template will work for npm run dev, but not so much for npm run build or npm run pack.

My temporary solution was to do modify HtmlWebpackPlugin in .electron-vue/webpack.web.config.js and .electron-vue/webpack.renderer.config.js:

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

The node version was upgraded from 10.16 to 14.0. This error occurred when executing 'NPM run dev'. We solved it this way. Perfect.

misterpancn avatar Jul 28 '20 10:07 misterpancn

Modyfying HtmlWebpackPlugin .electron-vue/webpack.web.config.js and .electron-vue/webpack.renderer.config.js does not work for me.

system: Linux Mint 19.3 nodejs: v14.7.0 npm: v6.14.7 webpack: v4.44.1

But https://auth0.com/blog/electron-tutorial-building-modern-desktop-apps-with-vue-js/ was very helpful

toudidel avatar Aug 04 '20 08:08 toudidel

I edit webpack.renderer.config.js as below:

new HtmlWebpackPlugin({
      filename: 'index.html',
      template: path.resolve(__dirname, '../src/index.ejs'),
      minify: {
        collapseWhitespace: true,
        removeAttributeQuotes: true,
        removeComments: true
      },
      isBrowser: false,
      isDevelopment: process.env.NODE_ENV !== 'production',
      nodeModules: process.env.NODE_ENV !== 'production'
        ? path.resolve(__dirname, '../node_modules')
        : false
    }),

and then change index.ejs like this:

<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="utf-8">
    <% if (htmlWebpackPlugin.options.nodeModules) { %>
        <!-- Add `node_modules/` to global paths so `require` works properly in development -->
        <script>
          require('module').globalPaths.push('<%= htmlWebpackPlugin.options.nodeModules.replace(/\\/g, '\\\\') %>')
        </script>
    <% } %>
</head>
<body>

<div id="app">
</div>

<!-- Set `__static` path to static files in production -->
<% if (!htmlWebpackPlugin.options.isBrowser && !htmlWebpackPlugin.options.isDevelopment) { %>
    <script>
      window.__static = require('path').join(__dirname, '/static').replace(/\\/g, '\\\\')
    </script>
<% } %>

<!-- webpack builds are automatically injected -->
</body>
</html>

it just drops "process" and works perfectly for me.

I hope this can help you.

it works for me

kingnand avatar Aug 19 '20 07:08 kingnand

add this code solved the problem for me

templateParameters(compilation, assets, options) {
        return {
          compilation: compilation,
          webpack: compilation.getStats().toJson(),
          webpackConfig: compilation.options,
          htmlWebpackPlugin: {
            files: assets,
            options: options
          },
          process,
        };
      }

where add this your code ?

...electron-vue\webpack.renderer.config.js ...electron-vue\webpack.web.config.js

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

Good,Thanks.

samchenws avatar Sep 03 '20 09:09 samchenws

I still can't get this to work after a clean install. This issue was opened over a year ago.

I guess writing quality code can be difficult sometimes ¯_(ツ)_/¯

pacific202 avatar Sep 05 '20 04:09 pacific202

got the same error, and solved it according to @wubzz 's solution, thank you!

jeepxiaozi avatar Oct 19 '20 04:10 jeepxiaozi

Experiencing the same issue on Node 12.2.0. Not sure what causes it, and not entirely convinced the issue is in this repo. Probably a dependency. Removing parts from the template will work for npm run dev, but not so much for npm run build or npm run pack.

My temporary solution was to do modify HtmlWebpackPlugin in .electron-vue/webpack.web.config.js and .electron-vue/webpack.renderer.config.js:

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

Work's for me on Node v14.14.0 Thanks Guys

willvitorino avatar Oct 19 '20 13:10 willvitorino

I think you should follow this article cum tutorial, it might be helpful! https://auth0.com/blog/electron-tutorial-building-modern-desktop-apps-with-vue-js/

Aryesh-M avatar Oct 28 '20 11:10 Aryesh-M

!require('process').browser

This works,thanks!

Aimee616 avatar Oct 29 '20 07:10 Aimee616

Oh my god, still got this error after a fresh installation. Node version: 14.15.4
solved by @wubzz 's solution.

sg552 avatar Jan 12 '21 09:01 sg552

Same here, still having the issue on Node v14.15.1 - will try @wubzz 's solution....

mklnz avatar Jan 20 '21 11:01 mklnz

nodejs/node#26882

Pretty much in node v12+ you need to explicitly import the process module.

Only change you need to make is below is update process to require('process'). I Can submit a PR if needed. No changes to your webpack config or anything is needed.

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>vest</title>
    <% if (htmlWebpackPlugin.options.nodeModules) { %>
      <!-- Add `node_modules/` to global paths so `require` works properly in development -->
      <script>
        require('module').globalPaths.push('<%= htmlWebpackPlugin.options.nodeModules.replace(/\\/g, '\\\\') %>')
      </script>
    <% } %>
  </head>
  <body>
    <div id="app"></div>
    <!-- Set `__static` path to static files in production -->
    <%  if (!require('process').browser) { %>
      <script>
        if (process.env.NODE_ENV !== 'development') window.__static = require('path').join(__dirname, '/static').replace(/\\/g, '\\\\')
      </script>
    <% } %>

    <!-- webpack builds are automatically injected -->
  </body>
</html>

It's works for me! Node v. 14.15.0

marker-dao avatar Mar 16 '21 15:03 marker-dao