Incoherent "Address already in use 127.0.0.1:XXXX" issue when using Ganache programmatically (JS)
See original GitHub issueI’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:
- Created 2 years ago
- Comments:7 (6 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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.
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