question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to use child_process?

See original GitHub issue

After creating a simple vue.js electron app with this plugin, I’m struggling with using e.g. exec from child_process.

In the HelloWorld.vue file, I try:

<script>
import { exec } from 'child_process'

export default {
  created() {
    exec('ls -lah /tmp', function(error, stdout, stderr) {
      console.log(stdout);
    });
  }
}
</script>

This results in

[Vue warn]: Error in created hook: "TypeError: Object(...) is not a function"

I’ve seen this https://github.com/nklayman/vue-cli-plugin-electron-builder/issues/121#issuecomment-433199340, but adding this to vue.config.js

module.exports = {
  configureWebpack: (config) => {
    config.externals = { 'child_process': 'require("electron").remote.require("child_process")' };
  },
};

causes this error:

ReferenceError: require is not defined

Further researches on this lead me to How to include Node modules with Webpack, but adding target:'electron-renderer' or target: 'node-webkit' as suggested here leads again to the require is not defined error. I also tried to prepend the previous externals with ...config.externals as I’ve found here, also with no luck.

When I import exec like this (removing all previous stuff again I changed, like the webpack settings):

const exec = require('electron').remote.require('child_process').exec;

And then call exec like above, I get a different error:

TypeError: fs.existsSync is not a function

Any idea how to properly import functions from child_process and call them from a Vue component with vue-cli-plugin-electron-builder?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:9

github_iconTop GitHub Comments

2reactions
Corben78commented, Apr 15, 2019

Oh dear… stupid me m( I was testing in the browser with npm run serve which of course doesn’t work, as the browser doesn’t allow it. Running via npm run electron:serve works. Without any changes to the webpack config. Just importing via import { exec } from 'child_process'; is enough.

1reaction
austenccommented, Dec 23, 2020

For folks coming here from a google search and thinking “but I have this correct in my vue.config.js file”:

Chances are you have a bug elsewhere which is causing this same Object(...) error! That was my problem, the solutions suggested in the docs page work correctly: https://nklayman.github.io/vue-cli-plugin-electron-builder/guide/security.html#node-integration

Read more comments on GitHub >

github_iconTop Results From Across the Web

Node.js Child Processes: Everything you need to know
The child_process module enables us to access Operating System functionalities by running any system command inside a, well, child process. We ...
Read more >
Child process | Node.js v19.3.0 Documentation
The child_process.spawn() method spawns the child process asynchronously, without blocking the Node.js event loop. The child_process.spawnSync() function ...
Read more >
Node.js Child Process - GeeksforGeeks
Node.js Child Process ; command: Accepts a string which is the command to run. args: List of string arguments. Default value is an...
Read more >
How To Launch Child Processes in Node.js - DigitalOcean
js applications. You'll create processes with the child_process module by retrieving the results of a child process via a buffer or string with ......
Read more >
Node.js Child Processes - javaTpoint
Node.js Child Process · child_process.exec() method: This method runs a command in a console and buffers the output. · child_process.spawn() method: This method ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found