Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

copy-webpack-plugin configuration issue

Within an electron.js app I'm trying to call and execute a python script from node.js based on the these indicatins: https://nodejs.org/api/child_process.html#child_process_child_process_execfile_file_args_options_callback

import { execFile } from 'child_process';

let pythonPath = '/home/marco/anaconda3/bin/python3';

execFile('./scripts/factorial.py',
  [parseInt(msg)],
  { cwd: './scripts',
    env: '/home/marco/anaconda3/bin/python3'
  },
  (error, stdout, stderr) => {
    if (error) {
      throw error;
    }
    console.log(stdout);
  }
);

And I get this error:

enter image description here

When commenting the options part:

execFile('./scripts/factorial.py',
  [parseInt(msg)],
  //{ cwd: './scripts',
    //env: '/home/marco/anaconda3/bin/python3'
  //},
  (error, stdout, stderr) => {
    if (error) {
      throw error;
    }
    console.log(stdout);
  }
);

I get the same error.

Following the indications found here: https://www.tutorialspoint.com/run-python-script-from-node-js-using-child-process-spawn-method I modified the code as follows:

const { spawn } = require('child_process');

let nb = parseInt(msg);

function runScript() {
  return spawn ('python3', [
    "-u",
    path.join(__dirname, './scripts/factorial.py'),
    nb,
  ]);
}

const subprocess = runScript();

subprocess.stdout.on('data', (data) => {
  console.log(`data:${data}`);
});

subprocess.stderr.on('data', (data) => {
  console.log(`error:${data}`);
});

subprocess.stderr.on('close', () => {
  console.log("Closed");
});

I get this error:

error:python3: can't open file '/home/marco/webMatters/electronMatters
/PythonConnection/.webpack/main/scripts/factorial.py': [Errno 2] No 
such file or directory

Closed

Following the indications found here: https://webpack.js.org/plugins/copy-webpack-plugin/#root I modified the /tools/webpack/webpack.plugins.js as :

 const CopyPlugin = require("copy-webpack-plugin");
module.exports = [
  new ForkTsCheckerWebpackPlugin(),
  new webpack.ExternalsPlugin('commonjs', [
    'electron'
  ]),
  new CopyPlugin({
    patterns: [
      { from: "src/scripts/", to: ".webpack/main/script/" },
    ],
  }),
];

To me, it seems respecting the standard, but I get this long error message:

An unhandled error has occurred inside Forge:
compilation.getCache is not a function
TypeError: compilation.getCache is not a function
    at /home/marco/webMatters/electronMatters/PythonConnection
/node_modules/copy-webpack-plugin/dist/index.js:459:33
    at SyncHook.eval (eval at create (/home/marco/webMatters
/electronMatters/PythonConnection/node_modules/tapable
/lib/HookCodeFactory.js:19:10),   <anonymous>:7:1)
at SyncHook.lazyCompileHook (/home/marco/webMatters/electronMatters
/PythonConnection/node_modules/tapable/lib/Hook.js:154:20)
at Compiler.newCompilation (/home/marco/webMatters/electronMatters
/PythonConnection/node_modules/webpack/lib/Compiler.js:630:30)
at /home/marco/webMatters/electronMatters/PythonConnection
/node_modules/webpack/lib/Compiler.js:667:29
at AsyncSeriesHook.eval [as callAsync] (eval at create (/home/marco
/webMatters/electronMatters/PythonConnection/node_modules/tapable
/lib/HookCodeFactory.js:33:10), <anonymous>:6:1)
at AsyncSeriesHook.lazyCompileHook (/home/marco/webMatters
/electronMatters/PythonConnection/node_modules/tapable
/lib/Hook.js:154:20)
at Compiler.compile (/home/marco/webMatters/electronMatters
/PythonConnection/node_modules/webpack/lib/Compiler.js:662:28)
at /home/marco/webMatters/electronMatters/PythonConnection
/node_modules/webpack/lib/Watching.js:77:18
at AsyncSeriesHook.eval [as callAsync] (eval at create (/home/marco
/webMatters/electronMatters/PythonConnection/node_modules/tapable
/lib/HookCodeFactory.js:33:10), <anonymous>:33:1)
at AsyncSeriesHook.lazyCompileHook (/home/marco/webMatters
/electronMatters/PythonConnection/node_modules/tapable
/lib/Hook.js:154:20)
at Watching._go (/home/marco/webMatters/electronMatters
/PythonConnection/node_modules/webpack/lib/Watching.js:41:32)
at /home/marco/webMatters/electronMatters/PythonConnection
/node_modules/webpack/lib/Watching.js:33:9
at Compiler.readRecords (/home/marco/webMatters/electronMatters
/PythonConnection/node_modules/webpack/lib/Compiler.js:529:11)
at new Watching (/home/marco/webMatters/electronMatters
/PythonConnection/node_modules/webpack/lib/Watching.js:30:17)
at Compiler.watch (/home/marco/webMatters/electronMatters
/PythonConnection/node_modules/webpack/lib/Compiler.js:244:10)
error Command failed with exit code 1.

It seems a step further, but still something to fix

node version: v14.5.0 OS: Ubuntu 18.04 Desktop

How to solve the problem? Looking forward to your kind help

like image 411
Raphael10 Avatar asked Jan 19 '21 17:01

Raphael10


People also ask

What does copy webpack plugin do?

webpack.config.js ℹ️ webpack-copy-plugin is not designed to copy files generated from the build process; rather, it is to copy files that already exist in the source tree, as part of the build process.

What is write file webpack plugin?

Forces webpack-dev-server program to write bundle files to the file system. This plugin has no effect when webpack program is used instead of webpack-dev-server .

What is a webpack plugin?

A webpack plugin is a JavaScript object that has an apply method. This apply method is called by the webpack compiler, giving access to the entire compilation lifecycle.


1 Answers

Seems that your problem is that you're using webpack < 5 and copy-webpack-plugin 7 which aren't compatibles.

Just do :

npm install copy-webpack-plugin@6 --save-dev

To fix it.

like image 190
Grégory L Avatar answered Oct 16 '22 01:10

Grégory L