2

I'm really having trouble trying to figure out how to use yahoo-finance with sveltekit. According to documentation, svelte doesn't allow require() syntax - it forces you to use import foo from "foo"

This is not normally much of a problem, but for some reason, when I try to import yahoo-finance, I get the following error:

util.inherits is not a function
TypeError: util.inherits is not a function
    at node_modules/tough-cookie/lib/memstore.js (http://localhost:3000/node_modules/.vite/yahoo-finance.js?v=dabb93e8:25313:10)
    at __require2 (http://localhost:3000/node_modules/.vite/chunk-ELXAK55F.js?v=dabb93e8:14:44)
    at node_modules/tough-cookie/lib/cookie.js (http://localhost:3000/node_modules/.vite/yahoo-finance.js?v=dabb93e8:25442:29)
    at __require2 (http://localhost:3000/node_modules/.vite/chunk-ELXAK55F.js?v=dabb93e8:14:44)
    at http://localhost:3000/node_modules/.vite/yahoo-finance.js?v=dabb93e8:60244:9
    at module2.exports (http://localhost:3000/node_modules/.vite/yahoo-finance.js?v=dabb93e8:15833:9)
    at node_modules/request-promise/lib/rp.js (http://localhost:3000/node_modules/.vite/yahoo-finance.js?v=dabb93e8:60241:17)
    at __require2 (http://localhost:3000/node_modules/.vite/chunk-ELXAK55F.js?v=dabb93e8:14:44)
    at node_modules/yahoo-finance/lib/utils.js (http://localhost:3000/node_modules/.vite/yahoo-finance.js?v=dabb93e8:61649:19)
    at __require2 (http://localhost:3000/node_modules/.vite/chunk-ELXAK55F.js?v=dabb93e8:14:44)
    at node_modules/yahoo-finance/lib/historical.js (http://localhost:3000/node_modules/.vite/yahoo-finance.js?v=dabb93e8:61822:18)
    at __require2 (http://localhost:3000/node_modules/.vite/chunk-ELXAK55F.js?v=dabb93e8:14:44)
    at node_modules/yahoo-finance/lib/index.js (http://localhost:3000/node_modules/.vite/yahoo-finance.js?v=dabb93e8:62433:27)
    at __require2 (http://localhost:3000/node_modules/.vite/chunk-ELXAK55F.js?v=dabb93e8:14:44)
    at node_modules/yahoo-finance/index.js (http://localhost:3000/node_modules/.vite/yahoo-finance.js?v=dabb93e8:62442:34)
    at __require2 (http://localhost:3000/node_modules/.vite/chunk-ELXAK55F.js?v=dabb93e8:14:44)
    at http://localhost:3000/node_modules/.vite/yahoo-finance.js?v=dabb93e8:62447:29

This error doesn't appear at runtime - it appears on page load in the browser.

Here is my full code.

<script>
    // var YahooFinance = require("yahoo-finance");    This doesn't work because require doesn't exist
    // import * as YahooFinance from "yahoo-finance"; This throws the same error as the next line
    import YahooFinance from "yahoo-finance";

    let text = "";
    const testQuote = async () => {
        const result = await YahooFinance.quote({
        symbol: 'AAPL',
        modules: [ 'price', 'summaryDetail' ] // see the docs for the full list
        });
        text = JSON.stringify(result, null, 2);
    }
</script>
<button on:click={testQuote}>Start Request</button>
<p>{text}</p>

2 Answers 2

0

There is an up-to-date, v2 version of this package that is currently maintained and that supports the ES module syntax.

However, the author also adds a caveat about using this module on the front-end. You will likely be facing CORS issues attempting to query Yahoo services directly from a browser client (and this is not an issue with the package, but rather a limitation imposed by Yahoo to limit 'unofficial' access). The only workaround is dispatching these calls from a server instead, meaning building your own proxy API.

Sign up to request clarification or add additional context in comments.

Comments

0

util.inherits() is a Node.js API, so if you're seeing it in the browser, it means that a polyfill for util is missing somewhere. How/why will depend on a lot of details, such as whether you are using webpack or browserify or what.

1 Comment

I'm using vite with sveltekit. Sorry, but I'm fairly new to web development, so I don't really know how to fix this based on what you have said above - google searching doesn't give any results with a fix, only stub threads and unanswered questions, or open github issues. Is it as simple as manually installing some packages? I've tried installing util and it now gives me an error saying that process does not exist

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.