1

I have an api route /auth/calculator and the handler spawns a child_process and then invokes a python script. The problem is my webpack bundler with nextjs doesn't copy the python file over to /build/server/pages/api/calculator and so when node attempts to access the python file it fails. How do I edit the webpack config rules so that it will see the .py extension and just copy the file over to /build/server/pages/api/calculator/calculator.py. Thanks!

const spawn = require("child_process").spawn;

const process = spawn("python3", [
  `${path.join(__dirname)}/calculator.py`     
  JSON.stringify(clientData),

]);

process.stdout.on("data", function (data) {
  return res.send(data.toString());
});

process.stderr.on("data", (data) => {
  console.error(`stderr: ${data}`);
});

process.on("close", (code) => {
  console.log(`child process exited with code ${code}`);
});
seanEd
  • 1,001
  • 1
  • 16
  • 29
  • Refer to [document on CopyWebpackPlugin](https://webpack.js.org/plugins/copy-webpack-plugin/). You can apply it to your webpack configuration to copy the python module over to the build folder. – Oluwafemi Sule Jul 08 '21 at 12:18

0 Answers0