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

ReferenceError: Stocks is not defined #14

Open
ilkertuna opened this issue Oct 14, 2017 · 3 comments
Open

ReferenceError: Stocks is not defined #14

ilkertuna opened this issue Oct 14, 2017 · 3 comments

Comments

@ilkertuna
Copy link

I am confused. I use stocks.js like

var Stocks = require("stocks.js");
var stocks = new Stocks('XXXX');       
                                                       

stocks.timeSeries({                                                 
    symbol: 'TSLA',                                                 
    interval: '1min',                                               
    amount: 10                                                      
}, function (err, res) {                                            
    console.log(res);                                               
}); 

Also, I installed stocks.js with npm.
However, I got an error "ReferenceError: Stocks is not defined"

Is there any suggestion?
Thanks in advance.

@ghost
Copy link

ghost commented Jan 7, 2018

This is how I got stocks.js to work on my setup using VueJS.

require('stocks.js')
const stocks = new Stocks('YOUR_API_KEY')

module.exports = {
  fetchData: symbol => {
    const options = {
      symbol: symbol,
      interval: '1min',
      amount: 1
    }

    return stocks.timeSeries(options)
  }
}

Where here I export the fetchData function and can pass it a symbol to then have it return the data.

However, I don't necessarily like having to use commonjs require statements as it throws this warning on webpack, Critical dependency: the request of a dependency is an expression.

Is there a way to not use the commonjs require with stocks.js and instead use ES6 import, but what would we be importing since import stocks from 'stocks.js' is not correct since you can't do stocks.Stocks('API_KEY')

So I think me and @ilkertuna are looking for the correct way to use stocksjs with ES6 imports as the examples look more focused on just <script> tag usage.

@ghost
Copy link

ghost commented Jan 7, 2018

Actually, it looks like PR #6 might solve this issue whenever that gets merged

However, until that gets resolved, what I've done, is placed the dist/stocks.js file in the static folder and then used a <script> tag in my index.html. This allows me to remove the require('stocks.js'), and avoids the webpack related error since stocks.js will be made available before I'm using it in my VueJS bundled application.

Maybe this will help someone with a similar issue regarding using stocks.js in webpack dependent projects.

@asemoon
Copy link

asemoon commented Sep 30, 2020

please add this to the ReadMe file it took me 30 minutes to figure out you have to do require('stocks.js') not require('stocks')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants