Search code examples
npmsvelteyahoo-financevitesveltekit

How do you use the yahoo-finance npm module with sveltekit?


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>

Solution

  • 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.