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

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

Open
karanbosamia opened this issue May 11, 2024 · 9 comments
Labels
bug Something isn't working

Comments

@karanbosamia
Copy link

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 karanbosamia added the bug Something isn't working label May 11, 2024
@gadicc
Copy link
Owner

gadicc commented May 13, 2024

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
Copy link

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
Copy link

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
Copy link
Owner

gadicc commented Jun 15, 2024

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
Copy link
Owner

gadicc commented Jun 15, 2024

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
Copy link

csherida commented Jun 15, 2024 via email

@gadicc
Copy link
Owner

gadicc commented Jun 15, 2024

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

@chefchefchef
Copy link

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

@csherida
Copy link

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants