alpacahq / alpaca-trade-api-js

Node.js library for Alpaca Trade API.
https://www.npmjs.com/package/@alpacahq/alpaca-trade-api
Apache License 2.0
509 stars 132 forks source link

[Bug]: importing alpaca-trade-api in react #234

Open Louvivien opened 1 year ago

Louvivien commented 1 year ago

Is there an existing issue for this?

Current Behavior

It is a create react app. I have installed alpaca-trade-api npm module and I try to import alpaca-trade-api in a React Component but I get this error :

Compiled with problems:X

ERROR in ./node_modules/@alpacahq/alpaca-trade-api/node_modules/dotenv/lib/main.js 24:11-24

Module not found: Error: Can't resolve 'fs' in '/Users/vivien/Documents/stock-trading-simulator/client/node_modules/@alpacahq/alpaca-trade-api/node_modules/dotenv/lib'

ERROR in ./node_modules/@alpacahq/alpaca-trade-api/node_modules/dotenv/lib/main.js 26:13-28

Module not found: Error: Can't resolve 'path' in '/Users/vivien/Documents/stock-trading-simulator/client/node_modules/@alpacahq/alpaca-trade-api/node_modules/dotenv/lib'

BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default.
This is no longer the case. Verify if you need this module and configure a polyfill for it.

If you want to include a polyfill, you need to:
    - add a fallback 'resolve.fallback: { "path": require.resolve("path-browserify") }'
    - install 'path-browserify'
If you don't want to include a polyfill, you can use an empty module like this:
    resolve.fallback: { "path": false }

ERROR in ./node_modules/urljoin/index.js 20:11-26

Module not found: Error: Can't resolve 'path' in '/Users/vivien/Documents/stock-trading-simulator/client/node_modules/urljoin'

BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default.
This is no longer the case. Verify if you need this module and configure a polyfill for it.

If you want to include a polyfill, you need to:
    - add a fallback 'resolve.fallback: { "path": require.resolve("path-browserify") }'
    - install 'path-browserify'
If you don't want to include a polyfill, you can use an empty module like this:
    resolve.fallback: { "path": false }

Do you know why?

Expected Behavior

No response

SDK Version I encountered this issue in

npm install --save @alpacahq/alpaca-trade-api

Steps To Reproduce

create react app
npm i @alpacahq/alpaca-trade-api
add this line to a react component: import Alpaca from '@alpacahq/alpaca-trade-api';

Filled out the Steps to Reproduce section?

Anything else?

No response

Louvivien commented 1 year ago

I think it is for backend only

304techmaven commented 1 year ago

@Louvivien I can confirm that this package is designed for backend. I was able to setup a basic express server and then create calls that would import different parts of the alpaca npm package and use them to authenticate, make trades etc.

Once this backend is setup via express you can then use any front end angular/react/vue etc. to make calls to this backend to make trades or turn bots on/off via UI elements in that web app. Hope this adds great context for anyone in the future!