Oleg
Oleg

Reputation: 41

How can I resolve the problem by using CoinGecko ApI: webpack < 5 used to include polyfills for node.js core modules by default

I am trying to use coinGecko API in my react app. I installed coingecko-api (npm install coingecko-api) and I created coinServices.js:


import CoinGecko from "coingecko-api";

const CoinGeckoClient = new CoinGecko();

export const fetchAllExchanges = async () => {
  try {
    const response = await CoinGeckoClient.exchanges.list();
    return response.data;
  } catch (error) {
    console.error("Error fetching all exchanges:", error);
    return []; 
  }
};

export const fetchAllCategories = async () => {
  try {
    const response = await CoinGeckoClient.coins.categories();
    return response.data;
  } catch (error) {
    console.error("Error fetching all categories:", error);
    return []; 
  }
};

export const fetchAllCoins = async () => {
  try {
    const response = await CoinGeckoClient.coins.markets({
      vs_currency: "usd",
    });
    return response.data;
  } catch (error) {
    console.error("Error fetching all coins:", error);
    return []; 
  }
};

export const fetchAllAssetPlatforms = async () => {
  try {
    const response = await CoinGeckoClient.assetPlatforms.list();
    return response.data;
  } catch (error) {
    console.error("Error fetching all asset platforms:", error);
    return []; 
  }
};  

Then I got errors like this:

ERROR in ./node_modules/coingecko-api/lib/CoinGecko.js 4:14-30
Module not found: Error: Can't resolve 'https' in 'D:\JavaScript\home\aleksashov\dev\PROJECTS\DashBoard\crypto-observer\node_modules\coingecko-api\lib'

BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default.
This is no longer the case. Verify if you need this module and configure a polyfill for it.

If you want to include a polyfill, you need to:
        - add a fallback 'resolve.fallback: { "https": require.resolve("https-browserify") }'
        - install 'https-browserify'
If you don't want to include a polyfill, you can use an empty module like this:
        resolve.fallback: { "https": false }

ERROR in ./node_modules/coingecko-api/lib/CoinGecko.js 5:20-42
Module not found: Error: Can't resolve 'querystring' in 'D:\JavaScript\home\aleksashov\dev\PROJECTS\DashBoard\crypto-observer\node_modules\coingecko-api\lib'

BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default.
This is no longer the case. Verify if you need this module and configure a polyfill for it.

If you want to include a polyfill, you need to:
        - add a fallback 'resolve.fallback: { "querystring": require.resolve("querystring-es3") }'
        - install 'querystring-es3'
If you don't want to include a polyfill, you can use an empty module like this:
        resolve.fallback: { "querystring": false }

Following instructions from these errors I've installed querystring-es3 and https-browserify and then i created webpack.config.js:

module.exports = {
    resolve: {
      fallback: {
        querystring: require.resolve("querystring-es3"),
        https: require.resolve("https-browserify"),
      },
    },
  };

For now I have the following package.json:

{
  "name": "crypto-observer",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@craco/craco": "^7.1.0",
    "@emotion/react": "^11.11.4",
    "@emotion/styled": "^11.11.0",
    "@fortawesome/free-solid-svg-icons": "^6.5.1",
    "@fortawesome/react-fontawesome": "^0.2.0",
    "@mui/icons-material": "^5.15.12",
    "@mui/material": "^5.15.12",
    "@reduxjs/toolkit": "^2.2.1",
    "@testing-library/jest-dom": "^5.17.0",
    "@testing-library/react": "^13.4.0",
    "@testing-library/user-event": "^13.5.0",
    "axios": "^1.6.7",
    "coingecko-api": "^1.0.10",
    "https-browserify": "^1.0.0",
    "node-polyfill-webpack-plugin": "^3.0.0",
    "querystring-es3": "^0.2.1",
    "react": "^18.2.0",
    "react-dom": "^18.2.0",
    "react-redux": "^9.1.0",
    "react-scripts": "5.0.1",
    "recharts": "^2.12.2",
    "redux": "^5.0.1",
    "redux-thunk": "^3.1.0",
    "web-vitals": "^2.1.4"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": [
      "react-app",
      "react-app/jest"
    ]
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  }
}

From the text of the error I see that(if I understood correctly) these issues are in \node_modules\coingecko-api\lib and tried to change Coingecko.js which is starts from such code as

'use strict';

//Modules
const https = require('https');
const querystring = require('querystring');
........

But it didn't bring any results... And I still have the same errors, despite all that I have done. A lot of thanks in advance for any help

Upvotes: 0

Views: 60

Answers (0)

Related Questions