Marcio Jorge
Marcio Jorge

Reputation: 23

How to use child_process in webpack

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.

Upvotes: 2

Views: 9948

Answers (2)

Thomas Wagenaar
Thomas Wagenaar

Reputation: 6749

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'
  ] 

Upvotes: 10

peteb
peteb

Reputation: 19418

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.

Upvotes: 4

Related Questions