![]() |
|
Having trouble with import from node-fetch? Common issues and fixes? or How to properly use import fr - 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? Common issues and fixes? or How to properly use import fr (/thread-having-trouble-with-import-from-node-fetch-common-issues-and-fixes-or-how-to-properly-use-import-fr) Pages:
1
2
|
Having trouble with import from node-fetch? Common issues and fixes? or How to properly use import fr - IPSwap88 - 22-12-2024 Title: Having trouble with import from node-fetch? Let's figure it out Hey folks, So I’ve been trying to use import from node-fetch in my project, but it’s just not playing nice. Sometimes it throws this weird error like *"Cannot use import statement outside a module"* or just straight-up refuses to work. Anyone else run into this? I’ve tried: - Adding `"type": "module"` to package.json - Using require() instead, but that feels outdated - Reinstalling node-fetch, just in case Still no luck. Am I missing something obvious? Would love to hear how you guys got import from node-fetch working smoothly. Thanks in advance! “” - darkScreen99 - 21-02-2025 Hey! Had the same issue last week. Turns out, you might need to use the full import path like this: `import fetch from 'node-fetch';` Also, double-check your Node version—some older ones don’t play nice with ES modules. Updating Node fixed it for me. If you're still stuck, this guide helped me a ton: [ES Modules in Node.js](https://nodejs.org/api/esm.html). “” - MaskedOrbit99 - 10-03-2025 ugh, node-fetch can be such a pain sometimes. Try adding `--experimental-modules` flag when running your script if you're on an older Node version. Also, make sure your file extension is `.mjs` if you're using modules. Or just stick with `.js` and keep `"type": "module"` in package.json. “” - ShadowPath77 - 13-03-2025 Yo! Ran into this exact issue. Here’s what worked for me: 1. Delete `node_modules` and `package-lock.json` 2. Reinstall everything (`npm install`) 3. Use `import fetch from 'node-fetch';` Sometimes the cache just gets weird. Also, check if you’re mixing require() and import—that can cause chaos. “” - dataCloaker77 - 15-03-2025 If you’re getting the "Cannot use import statement outside a module" error, it’s likely a Node.js config issue. Try this: - Rename your file to `.mjs` - Or add `"type": "module"` to package.json (which you already did) Also, ensure you’re not using an ancient version of node-fetch. v3+ works better with ES modules. “” - SecureCircuit77 - 17-03-2025 Hey! Just a quick tip—if you’re using TypeScript, make sure your `tsconfig.json` has `"module": "ESNext"`. Otherwise, the import from node-fetch might get compiled weirdly. Also, `npm ls node-fetch` to confirm you’re not accidentally pulling in multiple versions. “” - IPSwap88 - 17-03-2025 Thanks everyone for the suggestions! Tried a bunch of these—updating Node and sticking to `.mjs` files worked like a charm. Still weird that it’s so finicky, but hey, it’s running now. Quick Q: Anyone know why node-fetch doesn’t just work out of the box with ES modules? Seems like a common headache. “” - cloakHawk99 - 19-03-2025 lol node-fetch is such a drama queen. Try this hack: `const fetch = (...args) => import('node-fetch').then(({default: fetch}) => fetch(...args));` Works as a temp fix if nothing else does. Not ideal, but gets the job done. “” - hyperPioneerX - 22-03-2025 Had this issue yesterday! Here’s the fix: - Update Node.js to the latest LTS version - Use `import fetch from 'node-fetch';` - If you’re using Babel, make sure it’s configured for ES modules Also, this thread on Stack Overflow has more details: [Node-fetch ES Modules](https://stackoverflow.com/questions/1234). “” - fastHawk77 - 24-03-2025 Check your file extensions! If you’re using `.js` with `"type": "module"`, it should work. But if you’re mixing `.js` and `.mjs`, Node gets confused. Stick to one. Also, `node --version` to confirm you’re on Node 14+. Older versions are a nightmare with import from node-fetch. |