Webpack-dev-server: Cannot resolve module 'child_process' to build XMLHttpRequest

Created on 7 Oct 2014  路  5Comments  路  Source: webpack/webpack-dev-server


ERROR in ./~/parse/~/xmlhttprequest/lib/XMLHttpRequest.js
Module not found: Error: Cannot resolve module 'child_process' in /Users/aksonov/UPDT/sf/node_modules/parse/node_modules/xmlhttprequest/lib
@ ./~/parse/~/xmlhttprequest/lib/XMLHttpRequest.js 15:12-36

question

Most helpful comment

according to @jhnns 's answer, adding this to the externals in webpack.config.js fixed Parse for me.

externals:[{
    xmlhttprequest: '{XMLHttpRequest:XMLHttpRequest}'
}]

All 5 comments

It seems I can't use require('xmlhttprequest').XMLHttpRequest for web?

The xmlhttprequest module shims the XMLHttpRequest-API to node (for whatever reason - it's the ugliest API ever ^^). You wan't to bundle your code for the browser, and the browser doesn't know about processes and child processes.

There are two solutions to this problem:

  • Ask the maintainers of parse why they're using the xmlhttprequest module instead of node's http module.
  • Provide an alias for the xmlhttprequest module which just exports the original XMLHttpRequest object
module.exports = XMLHttpRequest;

Btw: This is not a webpack-dev-server issue.

according to @jhnns 's answer, adding this to the externals in webpack.config.js fixed Parse for me.

externals:[{
    xmlhttprequest: '{XMLHttpRequest:XMLHttpRequest}'
}]

Hi @jhnns I tried to use an alias for the xmlhttprequest module. The way I did is I created a dummy a.js file and I put module.exports = XMLHttpRequest; in it. In my make-webpack-config.js I have
var alias = {
xmlhttprequest$: "./a.js"
};
Now when I start production server I have error
module.exports = XMLHttpRequest;
^
ReferenceError: XMLHttpRequest is not defined
It seems webpack cannot find XMLHttpRequest (the original one supposedly is supported by the browser?) Is there something I'm missing?

Thanks!

this worked for me

const nodeExternals = require('webpack-node-externals');

module.exports = {
  //... webpack configs..
  externals: [nodeExternals()], // in order to ignore all modules in node_modules folder from bundling
};
Was this page helpful?
0 / 5 - 0 ratings

Related issues

tulika21-zz picture tulika21-zz  路  3Comments

da2018 picture da2018  路  3Comments

uMaxmaxmaximus picture uMaxmaxmaximus  路  3Comments

gimmi picture gimmi  路  3Comments

MJ111 picture MJ111  路  3Comments