question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Incoherent "Address already in use 127.0.0.1:XXXX" issue when using Ganache programmatically (JS)

See original GitHub issue

I’m having an issue with Ganache when trying to use it programmatically with a JavaScript script.

Basically it doesn’t want to start saying port is already in use - whereas it is not 🤷

It does start with the same port if I run the CLI directly.

How to reproduce:

  • create a script for Ganache (see code below)
  • run node ganache.js => does not work, results in “Address already in use 127.0.0.1:XXXX”, no matter the port
  • run ./node_modules/.bin/ganache -p XXXX => it works

Here’s the content of the script ganache.js - based on the example provided in README:

require("dotenv").config();
const ganache = require("ganache");
const Web3 = require('web3');

const options = {
  account_keys_path: "ganache.json",
  wallet: {
    accounts: [
      {
        secretKey: `${process.env.PRIVATE_KEY}`,
        balance: Web3.utils.toHex(`${process.env.INITIAL_BALANCE}`)
      }
    ],
  }
};

const port = process.env.PORT || 8545;
const server = ganache.server(options);
server.listen(port, async err => {
  if (err) throw err;
  console.log(`ganache listening on port ${port}...`);
  const provider = server.provider;

  await provider.request({
    method: "eth_accounts",
    params: []
  }).then(accounts => console.log(accounts));

});

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (6 by maintainers)

github_iconTop GitHub Comments

3reactions
jeffsmale90commented, Mar 16, 2022

Hi @MatthieuScarset - I just joined the team and picked up this case as a familiarisation task. Feel free to have a look at the change above - I tested with pretty much your exact snippet and it worked as expected.

3reactions
MatthieuScarsetcommented, Mar 11, 2022

Good catch! thank you, it seems that the issue was to cast to env

Apparently, parseInt(process.env.PORT) fixes the issues.

I’ve tried several different start/stop with different values and now Ganache “restart” correctly.

Thank you for your help!


For the record, here’s my implementation of Ganache programmatically: https://github.com/MatthieuScarset/quick-dapp/blob/develop/ganache.js

Read more comments on GitHub >

github_iconTop Results From Across the Web

npm run develop results in address already in use 127.0.0.1 ...
0.0 appears to fix it, but I won't know for sure as it happens erratically. Other time I see this error, which is...
Read more >
ganache-cli - bytemeta
Incoherent "Address already in use 127.0.0.1:XXXX" issue when using Ganache programmatically (JS). MicaiahReid. MicaiahReid OPEN.
Read more >
Function pointers show an unnecessary diff - Google/Go-Cmp
Incoherent "Address already in use 127.0.0.1:XXXX" issue when using Ganache programmatically (JS), 7, 2022-03-11, 2022-10-11.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found