[Ethereum] Truffle does not add “networks” to build/contracts/contract.json after migration

testrpctruffle

Problem

Truffle does not add "networks" to build/contracts/contract.json files after migration.

enter image description here


Versions running

  • Truffle v4.0.1 (core: 4.0.1)
  • Solidity v0.4.18 (solc-js)
  • EthereumJS TestRPC v6.0.3 (ganache-core: 2.0.2)
  • MacOS High Sierra 10.13.1

What I have tried already

  1. Downgrading Truffle:
    I have tried to downgrade from Truffle version 4.0.4 to version 4.0.0 and then to 4.0.1 to test if it has something to do with Truffle version, but unfortunately I get the same problem.

truffle.js file looks like this:

module.exports = {
   networks: {
     development: {
       host: "localhost",
       port: 8545,
       network_id: "*" // Match any network id
     }
   }
};
  1. I also tried to run over port 9545 with $ truffle develop, but got the same problem.

Error message

When I run $ truffle test I get the following error message:

Error: TestContract has not been deployed to detected network
(network/artifact mismatch)


What does work

When I run $ truffle migrate --reset, my build/contracts/Migrations.json file does have the networks specified like:

...
"networks": {
    "1516203204392": {
      "events": {},
      "links": {},
      "address": "0xa10a9e84eb3ef0bf7ebc1b6337c20031b5a80e69"
    }
  },
...

Best Answer

I had the same issue,

On closer observation, the issue is with 2_deploy_contracts.js. When you are running truffle migrate --reset Keep an eye on the text Saving artifacts... And also take a look at after which contract deployment this appears, and after this is the problem in the migration file.

I had some async/awaits in there and apparently the code executes but writing the artifacts has some time constraints and runs after the third contract deployment therefore only 3 of my contracts had networks populated and rest empty.

Hope this helps someone doing such silly mistakes like me.