【发布时间】: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