Webpack.js.org: require("child_process")

Created on 7 Sep 2016  路  9Comments  路  Source: webpack/webpack.js.org

_From @cowwoc on January 31, 2015 6:35_

I was attempting to package stripe.js when I ran across this line: https://github.com/stripe/stripe-node/blob/master/lib/stripe.js#L24

ERROR in ./~/stripe/lib/stripe.js
Module not found: Error: Cannot resolve module 'child_process' in ~\node_modules\stripe\lib
 @ ./~/stripe/lib/stripe.js 24:11-35

ERROR in ./~/stripe/lib/StripeResource.js
Module not found: Error: Cannot resolve module 'fs' in ~\node_modules\stripe\lib
 @ ./~/stripe/lib/StripeResource.js 14:31-44

If I understand correctly, these are two built-in modules that ship with node.js. Isn't webpack supposed to be able to provide a web-equivalent?

What do you recommend I do in this case?

_Copied from original issue: webpack/webpack#744_

Migrated

Most helpful comment

@TomHarker

I'm trying to build for electron.

electron workaround

webpack.config.js

//...
externals: {
  child_process: 'child_process'
}
//...

index.html

<script>
window.child_process = require('child_process');
</script>

my_program.js

const cp = require('child_process'); // its ok but...

All 9 comments

_From @sokra on January 31, 2015 15:56_

Isn't webpack supposed to be able to provide a web-equivalent?

Pretty difficult to spawn a process and access the filesystem in the web... So there is no replacement for these modules. Propably means you can't use this module in a browser...

_From @cowwoc on January 31, 2015 16:7_

What is the proper course of action?

I read elsewhere that I can do this:

node:
{
    "child_process": "empty"
}

but is there a better approach (keeping in mind that I can't modify the 3rd-party library)?

_From @sokra on January 31, 2015 16:11_

yes you can do that and hope that the module doesn't use any of the methods from this modules.

The proper course is the ask the module author to make the module browser compatible. The author can add browser: { fs: false, child_process: false } to the package.json to tell webpack that it's ok for the module to get an empty object for these modules.

_From @cowwoc on January 31, 2015 16:54_

Sounds good.

Is it possible to add this to the official documentation (i.e. "How to handle 3rd-party libraries with unresolved dependencies")? Otherwise, I assume other users will open issues asking the same question for different libraries.

_From @sheerun on November 26, 2015 13:58_

If you build for electron, you can add child_process to external modules

Hey! So, I just wanted to point out that the stripe Node module is only meant to be used server-side, as it requires using a secret key that should never be shared into the wild (on the web).

If you're looking to capture card details and other things from customers, Stripe.js can help you do that.

Closing this, what @floatingLomas said is right. You can't use a server side module on the web, and I don't think we can document this any clearer.

How are you supposed to know if a node module is browser compatible? And why should webpack care? I'm trying to build for NW.js

@TomHarker

I'm trying to build for electron.

electron workaround

webpack.config.js

//...
externals: {
  child_process: 'child_process'
}
//...

index.html

<script>
window.child_process = require('child_process');
</script>

my_program.js

const cp = require('child_process'); // its ok but...
Was this page helpful?
0 / 5 - 0 ratings