![]() |
|
Having trouble with import from node-fetch? How do I properly set it up in my project? - Printable Version +- Proxy Community (https://proxycommunity.com/forum) +-- Forum: Technical Community Support (https://proxycommunity.com/forum/forum-technical-community-support) +--- Forum: API and Development (https://proxycommunity.com/forum/forum-api-and-development) +--- Thread: Having trouble with import from node-fetch? How do I properly set it up in my project? (/thread-having-trouble-with-import-from-node-fetch-how-do-i-properly-set-it-up-in-my-project) Pages:
1
2
|
Having trouble with import from node-fetch? How do I properly set it up in my project? - HyperWarp99 - 21-07-2024 Hey y’all, So I’m trying to *import from node-fetch* in my project, but I keep getting errors. Like, it just won’t work?? I did `npm install node-fetch` and all that, but when I try to `import from node-fetch`, it’s like “module not found” or some weird syntax error. Am I missing something? Do I need to tweak my package.json or use some weird config? Also, I’m on Node 16, if that matters. Pls help, I’m lowkey stuck lol. Thanks in advance! “” - phantomNode99 - 22-11-2024 Hey! I had the same issue when trying to import from node-fetch. Turns out, in Node 16, you gotta use the ESM syntax. So instead of `import from node-fetch`, try `import fetch from 'node-fetch'` and make sure your package.json has `"type": "module"`. Also, double-check if you’re using the latest version of node-fetch. Sometimes npm installs an older version by default. Hope that helps! “” - vpnByteX - 18-01-2025 Yo, I feel your pain. Node 16 can be a bit picky with imports. If you’re getting a "module not found" error, it might be because node-fetch v3+ is ESM-only. Try downgrading to v2 with `npm install node-fetch@2` if you don’t wanna deal with ESM stuff. Alternatively, you could switch to CommonJS with `const fetch = require('node-fetch')`. Just depends on how you wanna structure your project. “” - DeepShield99 - 06-02-2025 Hey! Quick tip: if you’re using Node 16 and trying to import from node-fetch, make sure your file extension is `.mjs` or your package.json has `"type": "module"`. Node 16 treats ESM and CommonJS differently, and it can throw errors if it’s not set up right. Also, check out the official node-fetch docs—they’ve got a section on ESM usage that’s super helpful. “” - shadowRushX99 - 08-02-2025 Hmm, sounds like an ESM vs CommonJS issue. If you’re set on using `import from node-fetch`, you’ll need to enable ESM in your project. Add `"type": "module"` to your package.json, and make sure your import statement looks like `import fetch from 'node-fetch'`. If that doesn’t work, maybe try using `node --experimental-modules` to force ESM support. It’s a bit of a hassle, but it should get you going. “” - ghostTorX77 - 18-02-2025 Hey! I ran into this exact problem last week. If you’re on Node 16 and trying to import from node-fetch, you might wanna check if you’re using the correct syntax. ESM requires `import fetch from 'node-fetch'`, not `import from node-fetch`. Also, make sure your package.json has `"type": "module"` if you’re using ESM. If you’re still stuck, maybe try using `axios` instead—it’s a bit easier to set up and works great for HTTP requests. “” - MaskedLegendX - 23-02-2025 Yo, I had this issue too. If you’re getting errors when trying to import from node-fetch, it’s probably because node-fetch v3+ is ESM-only. You can either switch to CommonJS with `require('node-fetch')` or stick with ESM and add `"type": "module"` to your package.json. Also, make sure your Node version is up to date. Sometimes older versions of Node 16 can be buggy with ESM. Good luck! “” - Sekiro88 - 05-03-2025 Hey! If you’re stuck on importing from node-fetch, it’s likely an ESM issue. Node 16 handles ESM differently, so you’ll need to either: 1. Add `"type": "module"` to your package.json, or 2. Use `.mjs` file extensions for your modules. Also, check out this guide on ESM in Node.js—it’s super helpful: [link]. Let me know if you need more help! “” - HyperWarp99 - 10-03-2025 Yo, thanks for all the replies! I tried adding `"type": "module"` to my package.json and switched to `import fetch from 'node-fetch'`, and it worked! I didn’t realize Node 16 was so picky about ESM. Quick follow-up though: if I downgrade to node-fetch v2, will I lose any features? I’m kinda curious if it’s worth sticking with v3 or just switching to axios like someone suggested. Thanks again, y’all are lifesavers! “” - shadowNode77 - 12-03-2025 Hey, I had the same problem. If you’re trying to import from node-fetch and getting errors, it’s probably because node-fetch v3+ is ESM-only. You can either downgrade to v2 or enable ESM in your project by adding `"type": "module"` to your package.json. Also, make sure your import statement is correct: `import fetch from 'node-fetch'`. If you’re still stuck, maybe try using `undici`—it’s a newer alternative to node-fetch and works great with Node 16. |