【问题标题】:How do you use the yahoo-finance npm module with sveltekit?您如何将 yahoo-finance npm 模块与 sveltekit 一起使用?
【发布时间】:2021-11-17 22:16:10
【问题描述】:

我真的很难弄清楚如何将 yahoo-finance 与 sveltekit 一起使用。 根据文档,svelte 不允许 require() 语法 - 它强制您使用 import foo from "foo"

这通常不是什么大问题,但由于某种原因,当我尝试导入 yahoo-finance 时,我收到以下错误:

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

此错误不会在运行时出现 - 它会出现在浏览器的页面加载时。

这是我的完整代码。

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

【问题讨论】:

    标签: npm svelte yahoo-finance vite sveltekit


    【解决方案1】:

    util.inherits() 是一个 Node.js API,所以如果你在浏览器中看到它,这意味着 util 的 polyfill 在某处缺失。如何/为什么将取决于很多细节,例如您使用的是webpack 还是browserify 或什么。

    【讨论】:

    • 我正在使用 vite 和 sveltekit。抱歉,但我对网络开发还很陌生,所以我真的不知道如何根据你上面所说的来解决这个问题 - 谷歌搜索没有给出任何修复结果,只有存根线程和未回答的问题,或打开 github 问题。是否像手动安装一些软件包一样简单?我已经尝试安装 util,但它现在给我一个错误,说进程不存在
    【解决方案2】:

    目前有an up-to-date, v2 version of this package 维护并支持 ES 模块语法。

    但是,作者also adds a caveat about using this module on the front-end。尝试直接从浏览器客户端查询 Yahoo 服务时,您可能会遇到 CORS 问题(这不是软件包的问题,​​而是 Yahoo 为限制“非官方”访问而施加的限制)。唯一的解决方法是从服务器分派这些调用,这意味着构建您自己的代理 API。

    【讨论】:

      猜你喜欢
      • 2015-12-27
      • 1970-01-01
      • 2018-05-30
      • 1970-01-01
      • 2018-07-16
      • 2016-10-09
      • 2022-01-02
      • 2015-09-06
      • 2021-09-01
      相关资源
      最近更新 更多