使用“要求”;对于使用 npm 安装的模块上的浏览器端 js
我正在构建一个 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不能在浏览器中使用
require()
或node-fetch
。fetch()
已内置于浏览器中。就用那个吧。node-fetch
不在浏览器中运行。它基于 Nodejs 特定的代码(nodejs http 模块)构建,其工作是在浏览器中模拟fetch()
的实现。因此,只需使用浏览器自己的
fetch()
实现即可。这是文档。You can't use
require()
ornode-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 offetch()
in a browser.So, just use the browser's own
fetch()
implementation. Here's the doc.