2

I'm trying to run command line functions in a directory while hosting a web application, but when I try to run the following code, it throws

"Cannot resolve module 'child_process'":

var exec = require('child_process').exec;

It's running with Webpack.

Yves M.
  • 29,855
  • 23
  • 108
  • 144
Marcio Jorge
  • 23
  • 1
  • 5

2 Answers2

9

You have to make sure that child_process is not included in the bundle. Add it as an external, by adding the following to your webpack.config.js options:

  externals: [
    'child_process'
  ] 
Thomas Wagenaar
  • 6,489
  • 5
  • 30
  • 73
3

It's running with webpack.

Do you mean its been bundled with webpack?

Either way, if you're trying to bundle the child_process module for use client-side, you won't be able to. It needs the Node Core in order to run since it spawns/forks a new process at the os level. Its not going to automagically spawn a WebWorker or some browser equivalent.

The above is true for the other Node Core API's, they just aren't portable as they exist, they're meant to be run server-side. This github repo has a list of Node.js Core API's that have been ported to the browser.

peteb
  • 18,552
  • 9
  • 50
  • 62