我真的很難弄清楚如何通過 sveltekit 使用 yahoo-finance。根據檔案,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>
uj5u.com熱心網友回復:
util.inherits()是一個 Node.js API,所以如果你在瀏覽器中看到它,這意味著在util某處缺少一個 polyfill 。如何/為什么將取決于很多細節,例如您是否正在使用webpack或使用 browserify什么。
uj5u.com熱心網友回復:
這個包有一個最新的 v2 版本,目前正在維護并且支持 ES 模塊語法。
但是,作者還添加了一個關于在前端使用此模塊的警告。嘗試直接從瀏覽器客戶端查詢 Yahoo 服務時,您可能會遇到 CORS 問題(這不是軟體包的問題,??而是 Yahoo 為限制“非官方”訪問而施加的限制)。唯一的解決方法是從服務器分派這些呼叫,這意味著構建您自己的代理 API。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/360015.html
上一篇:MSTeams等應用程式如何更新
