Giter Site home page Giter Site logo

ReferenceError: fetch is not defined about d3-fetch HOT 4 CLOSED

d3 avatar d3 commented on April 28, 2024 2
ReferenceError: fetch is not defined

from d3-fetch.

Comments (4)

stevage avatar stevage commented on April 28, 2024 31

It's probably worth documenting how to make this work. I had to fiddle a bit, but this worked after npm install node-fetch-polyfill:

if (typeof fetch !== 'function') {
    global.fetch = require('node-fetch-polyfill');
}
const csv = require('d3-fetch').csv;

from d3-fetch.

mbostock avatar mbostock commented on April 28, 2024 14

You need an environment that supports the Fetch API, such as a a web browser. Node does not currently support Fetch, though it may in the future. If you want to load this library in an environment that does not natively support Fetch you will need to load your own polyfill such as node-fetch.

from d3-fetch.

jomasim avatar jomasim commented on April 28, 2024

You need to install jsdom using yarn or npm.
Node.js does not support dom by default, thus the need to use jsdom for creating dom elements. Use d3 for all other manipulations except fetch operations. i.e d3.xml,d3.tsv

import jsdom from 'jsdom';
import * as d3 from 'd3';
const { JSDOM } = jsdom;
JSDOM.fromURL(
    'your resource url',
  ).then((dom) => {
    const doc = dom.window.document;
    const states = d3
      .select(doc)
      .select('path')
      .attr(':fme:ID');
    console.log(states);
  });

creating dom from file

JSDOM.fromURL(
    'your resource url',
  ).then((dom) => {
    const doc = dom.window.document;
}

dom from html string

const dom = new JSDOM(
  `<p>Hello
    <img src="foo.jpg">
  </p>`,
  { includeNodeLocations: true }
);

from d3-fetch.

linglp avatar linglp commented on April 28, 2024

if (typeof fetch !== 'function') {
global.fetch = require('node-fetch-polyfill');
}

I have the following code after doing npm install node-fetch-polyfill

import express from 'express'
import cors from 'cors'
import { csv } from 'd3-fetch'

const app = express();

if (typeof fetch !== 'function') {
    global.fetch = require('node-fetch-polyfill');
}

But when I used the csv function, I still got ReferenceError: fetch is not defined error

Here's the code that works for me in the end:
I did npm install node-fetch, and then the following:

import express from 'express'
import cors from 'cors'
import { csv } from 'd3-fetch'

const app = express();
import fetch, {
    Blob,
    blobFrom,
    blobFromSync,
    File,
    fileFrom,
    fileFromSync,
    FormData,
    Headers,
    Request,
    Response,
} from 'node-fetch'

if (!globalThis.fetch) {
    globalThis.fetch = fetch
    globalThis.Headers = Headers
    globalThis.Request = Request
    globalThis.Response = Response
}

from d3-fetch.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.