node-yahoo-finance2 icon indicating copy to clipboard operation
node-yahoo-finance2 copied to clipboard

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

Open karanbosamia opened this issue 1 year ago • 9 comments

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

  • When starting the server with npm start, the package is blocking with error:
Failed to compile.

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

karanbosamia avatar May 11 '24 11:05 karanbosamia

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).

gadicc avatar May 13 '24 08:05 gadicc

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).

chefchefchef avatar May 20 '24 17:05 chefchefchef

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.

csherida avatar Jun 14 '24 19:06 csherida

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 avatar Jun 15 '24 13:06 gadicc

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.

gadicc avatar Jun 15 '24 13:06 gadicc

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: @.***>

csherida avatar Jun 15 '24 13:06 csherida

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

gadicc avatar Jun 15 '24 14:06 gadicc

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

chefchefchef avatar Jun 21 '24 17:06 chefchefchef

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!

csherida avatar Jun 21 '24 20:06 csherida