Javascript bot programming using Gemini API, NodeJS and CryptoCompare - DISCUSSION

I get the following error when I run node index.js. I’ve tried several different solutions that I googled, but nothing is working. I included my index.js and package.json below the error.

internal/modules/cjs/loader.js:1089
throw new ERR_REQUIRE_ESM(filename, parentPath, packageJsonPath);
^

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: c:\Users\lehi7\Blockchain\IvanTech\TradingBot\node_modules\node-fetch\src\index.js
require() of ES modules is not supported.
require() of c:\Users\lehi7\Blockchain\IvanTech\TradingBot\node_modules\node-fetch\src\index.js from c:\Users\lehi7\Blockchain\IvanTech\TradingBot\index.js is an ES module file as it is a .js file whose nearest parent package.json contains “type”: “module” which defines all .js files in that package scope as ES modules.
Instead rename c:\Users\lehi7\Blockchain\IvanTech\TradingBot\node_modules\node-fetch\src\index.js to end in .cjs, change the requiring code to use import(), or remove “type”: “module” from c:\Users\lehi7\Blockchain\IvanTech\TradingBot\node_modules\node-fetch\package.json.

global.fetch = require("node-fetch");
const GeminiAPI = require("gemini-api").default;
const secret = "2qXyFFYLC000000000000";
const key = "account-lT0A1000000000000";
const CrypAPIKey = "6111310028358589af0a900000000000000000000";
const restClient = new GeminiAPI({ key, secret, sandbox: true });

const CryptoCompareAPI = require("cryptocompare");
CryptoCompareAPI.setApiKey(CrypAPIKey);

restClient.newOrder({ amount: 10, price: 1000, side: "buy",symbol: "ethusd" })
.then(response => console.log(response))
.catch(error => console.error(error));

CryptoCompareAPI.coinList()
.then(coinList => {
    console.log(coinList)
})

/*restClient.getMyActiveOrders().then(response => console.log(response));*/


{
“name”: “tradingbot”,
“version”: “1.0.0”,
“description”: “crypto trading bot”,
“main”: “index.js”,
“scripts”: {
“test”: “echo “Error: no test specified” && exit 1”
},
“author”: “”,
“license”: “ISC”,
“dependencies”: {
“cryptocompare”: “^1.0.0”,
“gemini-api”: “^2.0.4”,
“node-fetch”: “^3.0.0”
}
}

1 Like

@Lehigr2

Hey Mate.

I am getting exactly the same error as you do, if you scroll a bit up you will see I have tried to use import instead of require and also try to use .cjs as the error says but no avail. I am awaiting if Carlos come up with somethin, I am also doing my research too but not so lucky this time. If you find a solution, could you post it and let us know? I will do the same!

Thank you.

1 Like

@thecil @Lehigr2

All I have found out now regarding many posts that if we want to use ES module then we need to type “type” : “module” in package.json and then we have to use the import * from “path” style because we cannot use the normal Javascript and the ES one mixed up. So is there anybody here who knows how to do that ES style module import?

1 Like

Hello, I am having difficulty finding a wrapper for Bitmex. The two main ones seem to have vulnerabilities. Any ideas??

I am having exactly the same problem, I read all the answers above and below, and I ll be keeping a track on this issue. Unfortunatly, I cannot advance on the course until someone can help us solve this.

Here is my console log and code:
algotrading> node index.js
internal/modules/cjs/loader.js:1102
throw new ERR_REQUIRE_ESM(filename, parentPath, packageJsonPath);
^

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: Z:\A-IVANONTECH ACADEMY\algotrading\node_modules\node-fetch\src\index.js
require() of ES modules is not supported.
require() of Z:\A-IVANONTECH ACADEMY\algotrading\node_modules\node-fetch\src\index.js from Z:\A-IVANONTECH ACADEMY\algotrading\index.js is an ES module file as it is a .js file whose nearest parent package.json contains “type”: “module” which defines all .js files in that package scope as ES modules.
Instead rename Z:\A-IVANONTECH ACADEMY\algotrading\node_modules\node-fetch\src\index.js to end in .cjs, change the requiring code to use import(), or remove “type”: “module” from Z:\A-IVANONTECH ACADEMY\algotrading\node_modules\node-fetch\package.json.

←[90m at Object.Module._extensions…js (internal/modules/cjs/loader.js:1102:13)←[39m
←[90m at Module.load (internal/modules/cjs/loader.js:950:32)←[39m
←[90m at Function.Module._load (internal/modules/cjs/loader.js:790:14)←[39m
←[90m at Module.require (internal/modules/cjs/loader.js:974:19)←[39m
←[90m at require (internal/modules/cjs/helpers.js:92:18)←[39m
at Object. (Z:\A-IVANONTECH ACADEMY\algotrading\index.js:1:16)
←[90m at Module._compile (internal/modules/cjs/loader.js:1085:14)←[39m
←[90m at Object.Module._extensions…js (internal/modules/cjs/loader.js:1114:10)←[39m
←[90m at Module.load (internal/modules/cjs/loader.js:950:32)←[39m
←[90m at Function.Module._load (internal/modules/cjs/loader.js:790:14)←[39m {
code: ←[32m’ERR_REQUIRE_ESM’←[39m
}

global.fetch = require(“node-fetch”);

//Conection to the TESTING gemini platform
const GeminiAPI = require(“gemini-api”).default;
//These are just TESTING NOT REAL MONEY NOR WALLET KEYS
const secret = “XXX”;
const key = “XXX”;
//This is a free api from crypto compare
const CrypAPIKey = “XXX”;
const restClient = new GeminiAPI({key, secret, sandbox:true});

const CryptoCompareAPI = require(“cryptocompare”);
CryptoCompareAPI.setApiKey(CrypAPIKey);

restClient.newOrder({amount:10, price:100, side:“buy”, symbol:“btcusd”})
.then(response => console.log(response))
.catch(error => console.log(error));

CryptoCompareAPI.coinList()
.then(coinList => {
console.log(coinList)
})

(ON MY CODE XXX IS REPLACED BY THE REAL KEYS OF COURSE)

I thank you in advance for any reply or assistance to whoever could give us a clue

1 Like

Hey guys @Riki, @Lehigr2, @JPLaclau, sorry for the delay.

Now this error according to my research comes from the node version that you could be running.

Some users has mention that the problem start to show on a specific node version:
https://github.com/standard-things/esm/issues/868

Now I’m not facing that error and my package.json is not that different than yours, but my node version is 12.
image

So please be sure that in your node module folder, you have the proper modules installed for your script:
image

Also you can downgrade your node version and try again your script. Your logic looks good, so i do not think the problem comes from a incorrect syntax in your code.

Now I’m using nvm to manage my node versions, it allows me to switch between node versions when i need, you can follow this guide if you are interesting on install it.
https://dev.to/skaytech/how-to-install-node-version-manager-nvm-for-windows-10-4nbi

Carlos Z

I think I had every error code on this forum. Every single one. and some that does not even appear here.

Now this is my new problem, I am getting really exhausted and frustrated. I would really thank in advance any assistance.

\algotrading> node index.cjs
internal/modules/cjs/loader.js:1015
throw new ERR_REQUIRE_ESM(filename, parentPath, packageJsonPath);
^

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: Z:\A-IVANONTECH ACADEMY\algotrading\node_modules\node-fetch\src\index.js
require() of ES modules is not supported.
require() of Z:\A-IVANONTECH ACADEMY\algotrading\node_modules\node-fetch\src\index.js from Z:\A-IVANONTECH ACADEMY\algotrading\index.cjs is an ES module file as it is a .js file whose nearest parent package.json contains “type”: “module” which defines all .js files in that package scope as ES modules.
Instead rename index.js to end in .cjs, change the requiring code to use import(), or remove “type”: “module” from Z:\A-IVANONTECH ACADEMY\algotrading\node_modules\node-fetch\package.json.

←[90m at Object.Module._extensions…js (internal/modules/cjs/loader.js:1015:13)←[39m
←[90m at Module.load (internal/modules/cjs/loader.js:863:32)←[39m
←[90m at Function.Module._load (internal/modules/cjs/loader.js:708:14)←[39m
←[90m at Module.require (internal/modules/cjs/loader.js:887:19)←[39m
←[90m at require (internal/modules/cjs/helpers.js:74:18)←[39m
at Object. (Z:\A-IVANONTECH ACADEMY\algotrading\index.cjs:1:16)
←[90m at Module._compile (internal/modules/cjs/loader.js:999:30)←[39m
←[90m at Object.Module._extensions…js (internal/modules/cjs/loader.js:1027:10)←[39m
←[90m at Module.load (internal/modules/cjs/loader.js:863:32)←[39m
←[90m at Function.Module._load (internal/modules/cjs/loader.js:708:14)←[39m {
code: ←[32m’ERR_REQUIRE_ESM’←[39m
}

global.fetch = require(“node-fetch”);

//Conection to the TESTING gemini platform
const GeminiAPI = require(“gemini-api”).default;
//These are just TESTING NOT REAL MONEY NOR WALLET KEYS
const secret = “XXX”;
const key = “XXX”;
//This is a free api from crypto compare
const CrypAPIKey = “XXX”;
const restClient = new GeminiAPI({key, secret, sandbox:true});

const CryptoCompareAPI = require(“cryptocompare”);
CryptoCompareAPI.setApiKey(CrypAPIKey);

restClient.newOrder({amount:10, price:100, side:“buy”, symbol:“BTCUSDT”})
.then(response => console.log(response))
.catch(error => console.log(error));

CryptoCompareAPI.coinList()
.then(coinList => {
console.log(coinList)
})

my node version is 12.20.0

I had to install that one because 12.0.0 and 10.x are not compatible with other stuff mentioned to install i am already to tired to retry and recopy, 12.20.0 is my node version

Hello Carlos(@thecil) and @Lehigr2 @JPLaclau

So at least it seems the error is gone. I have followed the instructions in that link and now I am using npm 12.0.0 and having no more issues with the ES module but I got this now:

New Error

I think the code refers to here:

new err2

So I have the same code but leaving here just in case:

global.fetch = require("node-fetch");
const GeminiAPI = require("gemini-api").default;
const secret = "uJVwHaon5Jrp9CB9LMYD8UFtucZ";
const key = "account-3mD9hql52ePPk7VuJFDR";
const CCAPIKey = "7070fb1cec78efbc4e88a389419001a97a0e232562f374c1321e3b03ebb7bc5f";
const restClient = new GeminiAPI({key, secret, sandbox:true});

const CryptoCompareAPI = require("cryptocompare");
CryptoCompareAPI.setApiKey(CCAPIKey);

restClient.newOrder({amount:10, price:100,side:"buy",symbol:"ethusd"})
.then(response => restClient.cancelOrder({order_id:response.order_id}))
.then(response => console.log(response))
.catch(error => console.log(error));


CryptoCompareAPI.coinList()
.then(coinList => {
  console.log(coinList)

})

So if I just remove that row the issue will go on to the next one, the https. So is this because now those are the ES modules and now this version cannot read those?

Yeah it’s getting out of hand I know but I really want to finish the course and I am trying to find a solution now for a week, always searching for answers for 1-2 hours almost every day.

I believe this importing thing is not compatible with the 11.0. it should all say require but that is only my opinion.

Is there any version where both the import and require is possible to use?

1 Like

I got the same exact issue. Tried the 10 version first and that didn’t work. I then tried Node 12.0.0 and I got the same errors

1 Like

Hi Ivan

I’m having trouble with trying to get the coin list from cryptocompare. Have entered the global.fetch = require(“node-fetch”); line at the top of the code, and then the command to get the coin list as written in the lesson. The following error message comes up on powershell:

internal/modules/cjs/loader.js:1089
throw new ERR_REQUIRE_ESM(filename, parentPath, packageJsonPath);
^

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: C:\Users\Dot\Documents\Cryptocurrency\Ivan on Tech\Algorithmic Trading\node_modules\node-fetch\src\index.js
require() of ES modules is not supported.
require() of C:\Users\Dot\Documents\Cryptocurrency\Ivan on Tech\Algorithmic Trading\node_modules\node-fetch\src\index.js from C:\Users\Dot\Documents\Cryptocurrency\Ivan on Tech\Algorithmic Trading\index.js is an ES module file as it is a .js file whose nearest parent package.json contains “type”: “module” which defines all .js files in that package scope as ES modules.
Instead rename C:\Users\Dot\Documents\Cryptocurrency\Ivan on Tech\Algorithmic Trading\node_modules\node-fetch\src\index.js to end in .cjs, change the requiring code to use import(), or remove “type”: “module” from C:\Users\Dot\Documents\Cryptocurrency\Ivan on Tech\Algorithmic Trading\node_modules\node-fetch\package.json.

←[90m at Object.Module._extensions…js (internal/modules/cjs/loader.js:1089:13)←[39m
←[90m at Module.load (internal/modules/cjs/loader.js:937:32)←[39m
←[90m at Function.Module._load (internal/modules/cjs/loader.js:778:12)←[39m
←[90m at Module.require (internal/modules/cjs/loader.js:961:19)←[39m
←[90m at require (internal/modules/cjs/helpers.js:92:18)←[39m
at Object. (C:\Users\Dot\Documents\Cryptocurrency\Ivan on Tech\Algorithmic Trading\index.js:1:16)
←[90m at Module._compile (internal/modules/cjs/loader.js:1072:14)←[39m
←[90m at Object.Module._extensions…js (internal/modules/cjs/loader.js:1101:10)←[39m
←[90m at Module.load (internal/modules/cjs/loader.js:937:32)←[39m
←[90m at Function.Module._load (internal/modules/cjs/loader.js:778:12)←[39m {
code: ←[32m’ERR_REQUIRE_ESM’←[39m
}

I have no idea what this all means. Has the code changed?

Would appreciate any help. Thanks a lot.

1 Like

This is very strange, i might need to download your project and try to replicate the error from my side.

The same for you guys @Lehigr2, @PhilD99.

It probably might be an npm version but i want to try some stuffs by my own, because my old project does not give me any error.

https://github.com/thecil/TA_AlgoCourse

Carlos Z

Thanks Carlos

Appreciate your help.

PhilD99

@thecil @Lehigr2 @PhilD99

Yes if you can check what I have right now I can send but github wont let me upload the node modules folder whic contains a lot of files.

I have a strong suspicion that the problem is when we add the cryptocompare part from yarn. (at least the version right now).

When you do the steps right now with today’s version are you still getting through?

So the question is, how do I send through all of my code to you? Because right now github not letting me because it is being too big.

Also I have tried to use your code but getting the same ES Module error.

I think the problem is on the npm version, I have kept with v12.0 because is the one that does not give me too many weird issues (like this one you guys are facing).

Now there is no need to upload the node_modules folder to github, with the package.json i can download your project and run npm install, then it will download me all the dependecies modules for your project to be able to run.

Also is good practice to not upload the node_modules folder, I always add it to the gitignore file which will not upload the folders or file that I name in it.

Carlos Z

Okay I have uploaded my 3 files, is that all you need right?

https://github.com/Riki0923/Algorithmic-Trading

1 Like

Perfect, I have managed to run your code properly. I think the issue is on the module node-fetch at the latest version, I just switch the version dependency in your package.json so I force it to download the same version that I have in my old version, and it does the trick.

image
NOTE: please remember to delete your node_modules folder and then change the version on the dependecies and then npm install again.

Then I was able to run your script properly :nerd_face:

image

@Lehigr2, @PhilD99, you guys can try the same.

Carlos Z

3 Likes

It works for me. I had actually changed my package.json to this version based on your github code. It didn’t work for me a few days ago, but after reading this post, I thought, you need to run npm install for the change to take effect. That did it, works fine. Thanks!

1 Like

Hi Carlos

Thanks for all that. I switched the “node-fetch” dependency on the package.json from “^3.0.0” to “^2.6.1” on atom. However, when I tried to execute node.js on Powershell I still got the same error message as I showed in my post on sept 11. Not sure where to go from here. Here’s my index.js code:

global.fetch = require(“node-fetch”);
const GeminiAPI = require(“gemini-api”).default;
const secret =“2xq5nUgLZgVsrjXuEHfqEyGGNqr1”;
const key = “account-Wn5E071DQtIZPyY69S5g”;
const CCAPIKey = “0875c533d882b9ea12e78d9a25e6db88ba3336313ae9aefdb6d35a2d2db87c1c”;
const restClient = new GeminiAPI({key, secret, sandbox:true});

const CryptoCompareAPI = require(“cryptocompare”);
CryptoCompareAPI.setApiKey(CCAPIKey);

CryptoCompareAPI.coinList()
.then(coinList => {
console.log(coinList)
})

Oh yeah finally got through! Thank you very much.

I am on the indicator creation part now and I am getting this error:

callback

code in indicator:

const CCAPIKey = "7070fb1cec78efbc4e88a389419001a97a0e232562f374c1321e3b03ebb7bc5f";
const CryptoCompareAPI = require("cryptocompare");
CryptoCompareAPI.setApiKey(CCAPIKey);

module.exports = {

movingAverage: function (cryptoAsset, fiatCurrency, hours){

  if(hours>169){
    console.error("Only up to 168 hours allowed!")
    return
  }
  // 1 get data from CC
  CryptoCompareAPI.histoHour(cryptoAsset, fiatCurrency)
  .then(data => {

    // 2 calculate MA from 100 past hours
    data = data.reverse()
    var sum = 0;
    for(var i = 0; i<100; i++){
      sum+=data[i].close
    }

    var movingAverage = sum/hours;
    callback(movingAverage);
  })
  .catch(console.error)
}


}

code on the main index file:

global.fetch = require('node-fetch')
const GeminiAPI = require("gemini-api").default;
const secret = "uJVwHaon5Jrp9CB9LMYD8UFtucZ";
const key = "account-3mD9hql52ePPk7VuJFDR";
const restClient = new GeminiAPI({key, secret, sandbox:true});
const indicators = require("./indicators.js");

/*restClient.newOrder({amount:10, price:100,side:"buy",symbol:"ethusd"})
.then(response => restClient.cancelOrder({order_id:response.order_id}))
.then(response => console.log(response))
.catch(error => console.log(error));*/

//100 hour MA

indicators.movingAverage("ETH", "USD", 100, function(){
  console.log("MA: ", result)
})

// 3 check continouslay if price is crossing 100 MA => BUY/SELL/uJVwHaon5Jrp9CB9LMYD8UFtucZ