-1

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!

  • Webpack, Browserify, Rollup, esbuild, ... – code Mar 19 '22 at 04:29
  • @code - But `node-fetch()` doesn't run in the browser as it's based on the nodejs http module so there's no point in using some packing tool. Besides, `fetch()` already exists in the browser which is what node-fetch is built to emulate. – jfriend00 Mar 19 '22 at 04:54
  • @jfriend00 in general, at least; but yes, fetch indeed. – code Mar 19 '22 at 04:56
  • Please provide enough code so others can better understand or reproduce the problem. – Community Mar 19 '22 at 06:19

1 Answers1

0

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.

jfriend00
  • 683,504
  • 96
  • 985
  • 979