0

I am Using node-fetch for some reason, I do it on simple HTMl and js not npm install. I want to use it with cdn. below attachments, I used this approach and not logging anything.

In html

<script type="module" src="module.js"></script>

In module.js

import nodeFetch from 'https://cdn.jsdelivr.net/npm/node-fetch@3.3.0/+esm';

console.log(nodeFetch)

console.log("asdsad"); // not log anything

WHat is the best way to use it with cdn, I confused, and tried to all day.

  • 1
    Have you tried using the built-in fetch and not node-fetch? Generally speaking, node modules will not work with HTML & browser based code. – EpicPuppy613 Feb 01 '23 at 16:52
  • Are there any errors in the console when you load this page. I have a feeling that node-fetch is not browser friendly and is throwing errors that are preventing you from getting to the console.log lines in your code. – NetByMatt Feb 01 '23 at 16:55
  • Yes, I tried with build-in fetch but I have an authentication error unauthenticated. Same Request I used in the js online compiler programiz, it quickly fetches the data in the online compiler. but in vs code, when I use the fetch method, I get an unauthenticated error, even if pass the correct Token with a header @EpicPuppy613 – HASEEB ALAM RAFIQ Feb 01 '23 at 17:17

1 Answers1

1

When I run that code I get the following errors reported in my console:

Loading failed for the module with source “https://cdn.jsdelivr.net/npm/data-uri-to-buffer@4.0.0/+esm”. localhost:7007:1:1
Loading failed for the module with source “https://cdn.jsdelivr.net/npm/fetch-blob@3.2.0/+esm”. localhost:7007:1:1
Loading failed for the module with source “https://cdn.jsdelivr.net/npm/formdata-polyfill@4.0.10/esm.min.js/+esm”. localhost:7007:1:1
Loading failed for the module with source “https://cdn.jsdelivr.net/npm/fetch-blob@3.2.0/from.js/+esm”.

Since the dependencies are failing, the script aborts before reaching console.log.

node-fetch doesn't appear to be a module designed to run client-side in a web browser!

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335