Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 BUG: Bundling with Stripe and other packages leads to unclear error hint #853

Closed
rishsriv opened this issue Apr 26, 2022 · 5 comments
Closed

Comments

@rishsriv
Copy link

What version of Wrangler are you using?

0.0.27

What operating system are you using?

Mac

Describe the Bug

I'm attempting to upload a bundle with the Stripe npm package and Durable Objects. But when attempting to upload, am getting error messages like these:

✘ [ERROR] Could not resolve "crypto"

    node_modules/stripe/lib/utils.js:5:23:
      5 │ const crypto = require('crypto');
        ╵                        ~~~~~~~~

  The package "crypto" wasn't found on the file system but is built into node. Are you trying to
  bundle for node? You can use "platform: 'node'" to do that, which will remove this error.

However, these do not specify where the platform: 'node' should be used (I have tried at various locations in wrangler.toml, webpack.config.js, and package.json, but nothing has worked so far)

The webpack config is below

const path = require('path');
const webpack = require("webpack");

module.exports = {
  // target: "javascript",
  entry: "./src/index.js",
  mode: "production",
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: "worker.js"
  },
  resolve: {
    extensions: ['.ts', '.tsx', '.js', '.mjs'],
    fallback: { 
      "child_process": false,
      "http": false,
      "https": false,
      "buffer": require.resolve("buffer"),
      "crypto": require.resolve("crypto-browserify"),
      "events": require.resolve("events"),
      "path": require.resolve("path-browserify"),
      "stream": require.resolve("stream-browserify"),
    }
  },
  plugins: [
    new webpack.DefinePlugin({
      STRIPE_API_KEY: JSON.stringify(process.env.STRIPE_API_KEY)
    }),
    new webpack.ProvidePlugin({
      // Polyfill Node behavior of Buffer being available at the global level.
      Buffer: ['buffer', 'Buffer'],
    }), 
  ]
};

Any help would be very much appreciated. Thanks!

@rishsriv rishsriv added the bug label Apr 26, 2022
@petebacondarwin
Copy link
Contributor

Hi @rishsriv - are you running the webpack bundling via a custom build in Wrangler2?

Apart from defining the STRIPE_API_KEY from the environment, I think that the Wrangler 2 built in bundling should just work with this, meaning that you could drop the webpack custom build step. Not sure the best approach to actually provide that API key at build time though...

@threepointone
Copy link
Contributor

The problem here is that stripe uses some node libs, which we don't currently polyfill. We want to do this very badly, but couldn't fit it into v2, but will definitely do so in 2.1 (tracking here #101) The workaround is to do a custom build like you're currently doing, so I don't know webpack well enough to understand why your polyfill isn't working/applying itself.

@rishsriv
Copy link
Author

Got it, thank you @petebacondarwin and @threepointone! Appreciate the responses. Will just wait for 2.1 then, I have offloaded Stripe authentication as a microservice in a VM for now.

Thanks again for the awesome work on this – working with wrangler2 has been a way better experience than wrangler. Better error hints ftw!

@threepointone
Copy link
Contributor

We published 0.0.28 that introduces a new experimental node_compat config / --node-compat flag. This might just fix issues with importing stripe and other packages that have dependencies on node builtins. Feel free to give it a spin, and let us know if you have an feedback or issues!

@threepointone
Copy link
Contributor

Closing this because I tried bundling a worker with stripe and it "worked". Happy to chat more if anything else comes up!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants