gadicc / node-yahoo-finance2

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

getting error when ng serve/ng build #50

Open santokt opened 3 years ago

santokt commented 3 years ago

ERROR in ./node_modules/yahoo-finance2/api/lib/fetchDevel.js Module not found: Error: Can't resolve 'crypto' in '/Users/*****/AngularProject/node_modules/yahoo-finance2/api/lib'

ERROR in ./node_modules/yahoo-finance2/api/lib/fetchDevel.js Module not found: Error: Can't resolve 'fs' in '/Users/***/AngularProject/node_modules/yahoo-finance2/api/lib'

ERROR in ./node_modules/yahoo-finance2/api/lib/fetchDevel.js Module not found: Error: Can't resolve 'path' in '/Users/**/AngularProject/node_modules/yahoo-finance2/api/lib'

advaiyalad commented 3 years ago

Node modules like fs, path, and crypto aren't being found, as angular runs in the browser. fetchDevel relies on the local filesystem, so it won't work in the browser. I think that the best solution is to just not use { devel: true }.

santokt commented 3 years ago

I am installing this package in a angular project and once we have data, I want to show in the UI.. Other yahoo finance APIs are working like that but not this one..so please fix this.

santokt commented 3 years ago

where can I remove this { devel: true }?

advaiyalad commented 3 years ago

Remove it from the "module options" parameter of any function (like search, autoc, historical, etc.). When you call something like await yahooFinance.search('AAPL', {}, { devel: true }), you are calling the search function and invoking the fetchDevel function instead of the regular fetch function. You are adding the { devel: true } there, and that doesn't work in the browser.

santokt commented 3 years ago

Still getting same error when you call like above.. I will wait until you actually fix this.

gadicc commented 3 years ago

Hey @santokt, thanks for your report. If this is happening without { devel: true } anywhere, it sounds like your bundler (ng) is trying to bundle fetchDevel.js even though it's conditionally imported, and without that condition being met. I had a quick look at the angular project and it seems that this is indeed an unsupported configuration. I'll put some thought into how else we can do this on our side and report back here.

I'll also note that this project is primarily intended to be run in node. You're going to have some other challenges running in the browser (CORS, etc). I'll add a note about that in the README (and track #54).

santokt commented 3 years ago

Yes I agree with you on Other issues like CORS. That we can handle by creating proxy. Have a look at this npm package stock info..here I can get data directly in Angular.(with cors issue)

https://www.npmjs.com/package/stock-info

gadicc commented 3 years ago

(deleted previous message)

@santokt, did you mean you're using a proxy with stock-info? I think I misunderstood you the first time. Ok so you have a proxy and you just need node-yahoo-finance2 to run in the browser without the fetchDevel error... will update you on this :)

gadicc commented 3 years ago

@santokt, think this is fixed now. If you're happy to run a development copy of the library, would be great if you could confirm (since I don't have any experience with angular's builder), but either way, pretty sure this will solve the issue and it will be in our next release later this week (you'll get an automatic notice here when that happens).

santokt commented 3 years ago

Sure.I can test the dev copy.how can I get and install that.Pls let me know and I will update you the test results.

gadicc commented 3 years ago

Thanks, @santokt, that will be great. I added some instructions at https://github.com/gadicc/node-yahoo-finance2/blob/devel/CONTRIBUTING.md#local-dev. If anything is not clear or doesn't work, let me know. If it's more complicated than you were expecting, feel free to just wait for the upcoming release.

santokt commented 3 years ago

Now i am not getting in installation, but while call getting below error.I guess you guys published the latest version..i just installed the latest version and tried to call in Angular.

validateAndCoerceTypes.js:143 Uncaught ReferenceError: process is not defined at Object../node_modules/yahoo-finance2/api/lib/validateAndCoerceTypes.js (validateAndCoerceTypes.js:143) at webpack_require__ (bootstrap:79) at Object../node_modules/yahoo-finance2/api/lib/moduleExec.js (moduleExec.js:22) at webpack_require (bootstrap:79) at Object../node_modules/yahoo-finance2/api/index-common.js (index-common.js:8) at __webpack_require (bootstrap:79) at Object../node_modules/yahoo-finance2/api/index-browser.js (index-browser.js:6) at webpack_require (bootstrap:79) at Module../src/app/yf/yf.component.ts (app.module.ts:19) at webpack_require (bootstrap:79)

gadicc commented 3 years ago

Thanks for reporting back, @santokt. Looks like @PythonCreator27 had you covered in #50 before I even got out of bed this morning :grin: So this is fixed now... it will be in the next release (probably in a few days again).

If you don't want to wait, I think you could probably just add the following line in your app somewhere before calling yahooFinance:

window.process = window.process || {};

just once, and in the main scope of a file, not inside a function or anything. To be clear, that's in your own app, not in the yahoo-finance source. Should side-step this issue for you until the next release is out.

advaiyalad commented 3 years ago

@santokt Has this been fixed in the latest version, or not?

santokt commented 3 years ago

Yes, I have tested and it worked ..Great job guys..Happy to help you on testing :)

image

gadicc commented 3 years ago

Awesome news! Thanks, @santokt, for reporting back. Shweet, let us know if you come across any other issues.

Thanks again to @PythonCreator27 for your patch on this and for following up :)

guaycuru commented 1 year ago

Looks like this issue is back. I'm using v2.4.3 with Angular and I'm getting these errors during build:

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

./node_modules/yahoo-finance2/dist/esm/src/lib/fetchDevel.js:4:0-20 - Error: Module not found: Error: Can't resolve 'fs' in '\node_modules\yahoo-finance2\dist\esm\src\lib'

./node_modules/yahoo-finance2/dist/esm/src/lib/fetchDevel.js:5:0-28 - Error: Module not found: Error: Can't resolve 'crypto' in '\node_modules\yahoo-finance2\dist\esm\src\lib'
Airmime commented 1 year ago

Hi All,

I also get this issue in v2.4.3 (with Angular 15.1.6).

Thanks

gadicc commented 1 year ago

Hey @guaycuru adn @Airmime, thanks for the updated reports.

I'll definitely need help with this as I don't know anything about Angular.

Did I understand correctly that you're trying to run this in the browser? In that case, I'm not sure why the Angular bundler is loading the node vs browser code. (As a side note, although you can run yf2 in the browser in theory, in practice, there are a lot of other gotchas for this, notably, you'll need a node or equivalent server regardless to bypass the CORS restrictions).