wagenaartje / stocks.js

:moneybag: stocks.js is an easy-to-use stock market API for Javascript
https://github.com/wagenaartje/stocks.js
MIT License
325 stars 69 forks source link

XMLHttpRequest require to use node target in webpack #4

Closed kresli closed 7 years ago

kresli commented 7 years ago

I need to add target: 'node' to my webpack config otherwise I've got this error

ERROR in ./~/xmlhttprequest/lib/XMLHttpRequest.js
Module not found: Error: Can't resolve 'fs'
ERROR in ./~/xmlhttprequest/lib/XMLHttpRequest.js
Module not found: Error: Can't resolve 'child_process'

I would suggest to use https://www.npmjs.com/package/whatwg-fetch. I can create a pull request

wagenaartje commented 7 years ago

Hmm. I'm not sure why this is happening. Travis CI seems to handle it fine. Are you sure using the linked library would fix this? What node version are you using?

kresli commented 7 years ago
node -v
v8.1.3

I created a sample repo. https://github.com/kresli/stocks-js-test. You can test it with npm run build

wagenaartje commented 7 years ago

Yep. Managed to reproduce it. I'm not really familiar with what target: 'node' does specifically. But you might be right in that we need to seek a new request module.

wagenaartje commented 7 years ago

I'm quite embarassed to say I never knew fetch existed. If you're sure that this doesn't create any problems (and if you're sure it fixes the current one), please do switch from XMLHttpRequest to the listed library.

It seems to work like a charm (run from page with appropiate content security policy):

fetch('https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol=MSFT&interval=1min&apikey=demo')
  .then(function (response) {
    return response.json()
  }).then(function (body) {
    console.log(body)
  });
wagenaartje commented 7 years ago

I made a version with fetch. I tested it, and it seems to have fixed the problem :+1:

parvezk commented 6 years ago

Follow this approach, it will solve the problem: https://github.com/webpack/webpack-dev-server/issues/66