[Ethereum] Cannot connect to ganache-cli with web3 and nodejs

ganachego-ethereumnodejsweb3js

from package.json:

"ganache-cli": "^6.3.0",
"web3": "^1.0.0-beta.41",

in my createContract.js

const Web3 = require("web3");
const ganache = require("ganache-cli");
console.log('ganache.provider :', JSON.stringify(ganache));
let web3 = new Web3(new Web3.providers.HttpProvider(ganache.provider()));

the result of the console log:

ganache.provider : {"_webpacked":true,"version":"2.4.0","to":{}}

my error:

.../node_modules/web3-providers/dist/web3-providers.cjs.js:99
    if (host.substring(0, 5) === 'https') {
             ^
TypeError: host.substring is not a function

any advice on this error ?

Best Answer

I think, you can use the ganache provider like this:

const web3 = new Web3(ganache.provider());