gadicc / node-yahoo-finance2

Unofficial API for Yahoo Finance
https://www.npmjs.com/package/yahoo-finance2
MIT License
353 stars 58 forks source link

'URLSearchParams' is not exported from 'url' (imported as 'URLSearchParams') #774

Open karanbosamia opened 1 month ago

karanbosamia commented 1 month ago

Bug Report

ERROR in ./node_modules/yahoo-finance2/dist/esm/src/env-node.js 5:2-17 export 'URLSearchParams' (imported as 'URLSearchParams') was not found in 'url' (possible exports: Url, format, parse, resolve, resolveObject

Describe the bug

Attempted import error: 'URLSearchParams' is not exported from 'url' (imported as 'URLSearchParams'). ERROR in ./node_modules/yahoo-finance2/dist/esm/src/env-node.js 5:2-17 export 'URLSearchParams' (imported as 'URLSearchParams') was not found in 'url' (possible exports: Url, format, parse, resolve, resolveObject)


## Minimal Reproduction
- Starting the server with `npm start`

## Environment
Browser or Node: {"react": "^17.0.2"}
Node version (if applicable): 
Npm version: "^10.4.0"
Browser verion (if applicable):
Library version (e.g. `1.10.1`): 

## Additional Context
gadicc commented 1 month ago

Hey @karanbosamia

Do you by chance have a package called url installed? (i.e. does node_modules/url exist?). It shouldn't. Unless you need this package for something else, it's interfering with yahoo-finance2 from importing from node's built in url module.

Also, what framework are you using? (NextJS, etc).

chefchefchef commented 1 month ago

I have the same issue, cannot use yahoo-finance2 .

I do not have url installed and also do not have anything like node_modules/url. I use angular v17. Also doesn't work with url installed (as you have mentioned).

csherida commented 2 weeks ago

Hi @gadicc - really nice work here. I am also seeing this error running a next.js client-side component. Interestingly, I still receive data back, unless I have the fetch wrapped in a useEffect() hook. If I keep the call synchronous though, I get data back. I confirmed I don't have any node_modules/url* packages installed.

Appreciate any ideas you have.

gadicc commented 1 week ago

Thanks all, for the reports and kind words. I'm just going to be honest here and say that I don't currently have time to look into this further - for now. There are a few big changes in progress (thanks to the awesome our contributor community) that will make it a lot easier to get yf2 running in non-node environments. That will still primarily be denon and v8 for edge computing, because the browser security model has a lot of restrictions, some of which are unsolveable. So, I strongly, strongly recommend in any case, to run yf2 on the server/serverless side.

In nextjs this should be as simple as putting it in a server component (i.e., not a client component); however, I haven't tried that myself, and sometimes nextjs's magical bundling can break things. But it definitely works in nextjs api routes, and it's fairly trivial to have the client side call such a route.

Trivial if you don't need type safety, at least. For type safety, you'll need something like https://trpc.io/ or here's e.g. a codesandbox without it, I guess we should make something official like that which could be easily imported, but for now, hopefully you'll get the idea `:) If you have questions on the codesandbox, please open another issue for it and I'll be able to explain.

You basically get to do this in your client-side React component:

const result = useYahooFinance("quote", "TSLA");
gadicc commented 1 week ago

However, the react server component option really makes the most sense though, so try that first, and if no luck, I'll try do a codesandbox for that too if I get a chance.

csherida commented 1 week ago

Hi Gadi,

I can look at it in more detail next week if you like. I was able to get around it by creating an internal API that wrapped your component.

Thanks, Chris

On Jun 15, 2024, at 9:54 AM, Gadi Cohen @.***> wrote:



However, the react server component option really makes the most sense though, so try that first, and if no luck, I'll try do a codesandbox for that too if I get a chance.

— Reply to this email directly, view it on GitHubhttps://github.com/gadicc/node-yahoo-finance2/issues/774#issuecomment-2169691258, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AB2UOWHGP6ZAOS5A5TYF6ODZHRBP5AVCNFSM6AAAAABHR3D7GSVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCNRZGY4TCMRVHA. You are receiving this because you commented.Message ID: @.***>

gadicc commented 1 week ago

Ah, great! No worries then, as long as you're up and running 🎉 😁

chefchefchef commented 1 week ago

@csherida can you explain a little bit in more detail how you achieved to run the library? thanks in advance!

csherida commented 1 week ago

Sure... I'm using Next.js 14.x (with app routing). Under the API folder, I created an API route. From there, I used useEffect to fetch the data from my API which wrapped yahooFinance2 and formatted the data.

I put my code on my GitHub: https://github.com/csherida/next-stock-chart

Let me know if it works. Good luck!