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]: unable to use in a Cloudflare worker environment because fetch is not defined #222

Open
2 tasks done
taylorcode opened this issue Aug 2, 2022 · 0 comments
Open
2 tasks done

Comments

@taylorcode
Copy link

Is there an existing issue for this?

  • I have searched the existing issues

Current Behavior

import Alpaca from '@alpacahq/alpaca-trade-api';

Inside of a cloudflare worker’s code fails with:

Uncaught TypeError: globalThis.XMLHttpRequest is not a
  constructor

when running the Cloudflare CLI wrangler publish.

The reason that this fails is because the cloudflare worker V8 runtime environment does not support XMLHttpRequest. It only supports fetch.

I think that the source of the dependency between the Alpaca library and XMLHttpRequest is the Axios library.

Perhaps Alpaca can check for the existence of the fetch global and fall back to axios for IE support.

Expected Behavior

Importing the Alpaca package into a cloudflare worker’s code does not cause wrangler publish to fail.

SDK Version I encountered this issue in

2.16.1

Steps To Reproduce

1. Create a cloudflare account
2. Set up a cloudflare worker with a custom build script that bundles the worker’s dependencies
3. Set node_compat = true in the the worker’s wrangler.toml configuration file to enable polyfills for node builtins
4. Install the alpaca library with npm or yarn
5. Import alpaca into the worker’s code
6. Run wrangler publish

Filled out the Steps to Reproduce section?

  • I have entered valid steps to reproduce my issue or have attached a minimally reproducible case in code that shows my issue happening; and understand that without this my issue will be flagged as invalid and closed after 30 days.

Anything else?

No response

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

1 participant