使用“要求”;对于使用 npm 安装的模块上的浏览器端 js

发布于 2025-01-15 11:29:48 字数 232 浏览 3 评论 0原文

我正在构建一个 Figma 插件,需要向另一个 API 发出 POST 请求,因此我使用 npm i node-fetch 安装了节点包“node-fetch”,并尝试使用以下命令将其引入我的项目: const fetch = require('节点获取');

但是,我收到“ReferenceError:'require'未定义”。据我了解,Figma 插件在类似浏览器的环境中运行。在这种情况下我该如何使用 require ?谢谢!

I’m building a Figma plugin and need to make a POST request to another API, so I installed the node package ‘node-fetch’ with npm i node-fetch, and am trying to bring it into my project with: const fetch = require(‘node-fetch’);

However, I’m getting “ReferenceError: ‘require’ is not defined”. Figma plugins, by my understanding, run in a browser-like environment. How do I use require in this context? Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

¢蛋碎的人ぎ生 2025-01-22 11:29:48

您不能在浏览器中使用 require()node-fetch

fetch() 已内置于浏览器中。就用那个吧。

node-fetch 不在浏览器中运行。它基于 Nodejs 特定的代码(nodejs http 模块)构建,其工作是在浏览器中模拟 fetch() 的实现。

因此,只需使用浏览器自己的 fetch() 实现即可。这是文档

You can't use require() or node-fetch in a browser.

fetch() is built-into a browser already. Just use that.

node-fetch does not run in a browser. It's built on nodejs-specific code (the nodejs http module) and its job is to emulate the implementation of fetch() in a browser.

So, just use the browser's own fetch() implementation. Here's the doc.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文