Using node library on isomorphic React / Redux / Webpack

时间:2016-07-11 22:07:04

标签: reactjs webpack redux webpack-dev-server

I am working on an isomorphic react project and am attempting to use the Stripe node SDK (vs their REST API) within a Redux module. When Webpack builds the project I get the error: Module not found: Error: Cannot resolve module 'child_process'. I know this is a node only module, so I cannot use it on the browser (I only want Stripe API calls to originate on the server-side), I just don't know how to ensure that the Stripe node_module is not packaged in to the browser app.js file.

While I am importing the stripe module in the redux module, on the JSX side, I am only importing the IStripe interface from the stripe.model and the getPlansList dispatcher from my stripe redux module.

So my question is how does one use node-only modules within an isomorphic React project? Do I need to run a separate server-only node process that handles my Stripe calls? Or is it possible to contain this functionality within my isomorphic app?

Thanks!

1 个答案:

答案 0 :(得分:0)

查看the issue,您不应在浏览器中运行stripe-node

您可以在webpack config中设置alias

{
    resolve: {
        alias: {
            'stripe': './src/stripe-browser'
        }
    }
}

如果您不需要在浏览器中使用Stripe,则只需创建一个空文件stripe-browser作为API的存根。

如果您要在浏览器中使用API​​,则必须为Stripe.js客户端库创建适配器。