Search code examples
node.jsreactjswebpackcreate-react-app

Module not found : 'child process'


I'm developing a ReactJS app with Babel and Webpack. I am using the create-react-app facebook script so it handles the Webpack´s configuration. My problem is that I created a js file and add:

var childProcess = require('child_process');

But when I want to compile the new version i get the following error :

Module not found: 'child_process'.

I don't know what to do with this . I have read that adding custom configurations to the webpack.config.js may be the solution but i am using create react app so I don't have the Webpack configuration. I tried running npm run eject and create my own webpack.config.js but it doesn't work.

I hope somebody could help me.


Solution

  • You need to configure the correct target inside the webpack configuration: https://webpack.github.io/docs/configuration.html#target

    module.exports = {
      entry: './path/to/my/entry/file.js',
      ...
      target: 'node',// we can use node.js modules after adding this configuration
    };