我真的很难弄清楚如何在sveltekit中使用yahoo-finance。根据文档,import foo from "foo"不允许使用svelte语法-它迫使你使用svelte
这通常不是什么大问题,但由于某些原因,当我尝试导入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>发布于 2021-11-17 22:22:49
util.inherits()是一个Node.js应用编程接口,所以如果您在浏览器中看到它,这意味着某个地方缺少util的polyfill。方式/原因将取决于很多细节,例如您使用的是webpack还是browserify等。
发布于 2021-11-18 12:06:05
当前维护的an up-to-date, v2 version of this package支持ES模块语法。
然而,作者also adds a caveat about using this module on the front-end。您可能会面临CORS问题,试图直接从浏览器客户端查询Yahoo服务(这不是软件包的问题,而是Yahoo为限制“非官方”访问而施加的限制)。唯一的解决办法是从服务器分派这些调用,这意味着构建您自己的代理API。
https://stackoverflow.com/questions/70012257
复制相似问题