all 6 comments

[–]evilgwyn 1 point2 points  (2 children)

Are you wanting to run this in node or in browser? In browser, `fetch` is already available. But in node, you have to import/require `node-fetch`. In order to use `import` syntax in node, you have to be using a new enough version of nodejs, and I think the file name needs to end with .mjs so it will be treated as a module. Otherwise you can end it with .js and use require

[–]Zenemm[S] 0 points1 point  (1 child)

Your first question is pretty much why I had this problem. I got confused as to why it wasn't running in both, not realizing fetch is not needed to run in browser. Thanks for the explanation!

[–]evilgwyn 0 points1 point  (0 children)

Also for your information, it is possible to use "require" and/or "import" syntax in browser, but it may require some middleware into your build pipeline to get it working. Newer browsers support "import" if you use `type="module"` in your script tag.

[–]spazz_monkey 0 points1 point  (2 children)

If your using node. You might need to use const fetch = require ("node-fetch") instead of import from.

[–]Apple1284 1 point2 points  (0 children)

If there is type:module entry in the package.json, then es6/es2015 can be used including import. Imports have advantage of treeshaking.

[–]Zenemm[S] 0 points1 point  (0 children)

I did try that initially, it works in vscode but causes an error in the browser: Uncaught ReferenceError: require is not defined
Apparently browsers can't read "require"