'Using ‘require’ for browser-side js on module installed with npm
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!
Solution 1:[1]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
