# Truffle Webpack starts with error: MetaCoin has not been deployed to detected network

I seem to have followed the truffle guide to a T. But when I open my app in a browser for the first time, I'm getting this error in the console:

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


I'm brand new to Ethereum development so I'm mostly just flailing around in the dark here. Any help would be greatly appreciated.

The steps I've followed:

(1) installed truffle and testrpc

npm install -g truffle

npm install -g ethereumjs-testrpc

(2) initialized a webpack project

truffle init webpack

(3) started up testrpc

testrpc

(4) compiled contracts

truffle compile

(5) deployed contracts (I could see them hitting testrpc in the console)

truffle migrate

(6) started development web server

npm run dev

Possibly relevent: I'm running MetaMask in Chrome and I've logged into an account. That doesn't seem to change the issue.

• I don't face any issues, following the same commands on my machine. which version of truffle are you using? – Sanchit Jun 29 '17 at 3:03
• I'm using testrpc - 3.0.5 and Truffle - 3.2.5. – Sanchit Jun 29 '17 at 3:04

One thing to check would be the network ID. It doesn't matter what network ID you use for your local test node, but Truffle needs to agree about what you're using with testrpc.

With testrpc running, do: curl -H "Content-Type: application/json" -X POST --data '{"id": "1", "jsonrpc":"2.0","method":"net_version","params":[]}' http://localhost:8545

This should tell you the network ID that testrpc is using; Recent versions seem to be using what looks like a timestamp from when it was started.

Then take a look in the contract definition files under truffle/build/contracts and see if they have the same network ID specified as the key in the networks section. If they don't, you might want to try specifying the network ID in your truffle.js and redeploying. (By default this uses "*", which should work it out automatically, but I've had problems with it occasionally for reasons I never got to the bottom of.) Superstitiously, I would also delete the contents of the build directory and run truffle deploy again.

If this turns out to be the problem, you can force testrpc to use the same network ID every time by running it with the --network-id flag, eg to use the network ID 1337 you would run testrpc --network-id 1337.

• If you're using ganache, do ganache-cli --networkId. – Paul Berg Sep 18 '18 at 21:47
• If you are running a private network, you can also look it up in the genesis.json under chainId – saitam Jan 20 at 10:10

I ran into the same error, the issue I faced was that even though I was recompiling my Contract, it was not getting migrated onto the network. Then running truffle-contract deployed on the FE, it could not find the contract on the network. Is it possible that your contract got deployed and then you tried to redeploy it and then you got this error?

The fix was to add a migration script in the /migrations directory.

Create a new file called n_metacoin_migration.js with the below content: (where n is the next number as found in list of files under 'migrations' directory. For example, it would be like 1_initial_migration.js, 2_myown_contract_migration.js, etc.)

var MetaCoin = artifacts.require("./MetaCoin.sol");

module.exports = function(deployer) {
deployer.deploy(MetaCoin);
};


Then run the migration script in your command line.

truffle compile
truffle migrate


this was the content of my truffle:

networks: {
development: {
host: '127.0.0.1',
port: 7545,
network_id: '*' // Match any network id
}
}


The port should have been 8545 which is where the testrpc runs.

You can fix the error by changing the port to "8545", same as the testrpc, run truffle migrate --reset and that should fix it.

restart the node server also

• In my case, I needed to do this and also to create the migration script. – Jorge M. Londoño P. Oct 16 '18 at 2:31

When metamask is running in the browser then web3 takes metamask or other defined web3 like Mist if none of them is found then it searches for localhost.

if (typeof web3 !== 'undefined') {
console.warn("Using web3 detected from an external source. If using MetaMask, see the following link. Feel free to delete this warning. :) http://truffleframework.com/tutorials/truffle-and-metamask")
window.web3 = new Web3(web3.currentProvider);
} else {
console.warn("No web3 detected. Falling back to http://127.0.0.1:9545. You should remove this fallback when you deploy live, as it's inherently insecure. Consider switching to Metamask for development. More info here: http://truffleframework.com/tutorials/truffle-and-metamask");
// fallback - use your fallback strategy (local node / hosted node + in-dapp id mgmt / fail)
window.web3 = new Web3(new Web3.providers.HttpProvider("http://127.0.0.1:9545"));
}


To make it work:
Turn off the metaMask plugin from manage extension
or

In my case ..

"networks": {}


was empty for my contract in folder build/contracts/{mycontract.js}

Fix was to register in migrations folder . Register and deploy mycontract.

I had the same error. My issue was that one contract.json file did not have an address. Try linkng the contracts.json in the build directory and the src directory. One of them may not have an address.

try running a script

add this this script to client/package.json

"link-contracts":"cd src && ln -s ../../build/contracts contracts"


then

npm run link-contracts
truffle migrate --reset --all


If you are not using npm run dev to serve your app you'll have to run npm run watch in a terminal to ensure that your compiled assets point to the latest deployment.

"truffle migrate" corrected the wrong network id inside my_project/build/contracts/xyz.json ... as mentioned by Edmund Edgar above.

If you have to, delete all files inside my_project/build/contracts/ then run "truffle migrate" again

Write "truffle unbox webpack" instead. Worked for me :)

I ran into more or less the same issue last night and eventually found my problem to be that my metamask wasn't looking at the right test network. Having gone round the houses a few times I eventually realised that you need to point your metamask to look at the correct (custom RPC) network pointed at the correct truffle test port (in my case localhost:9545) and the configuration file (./truffle.js) set up correctly:

module.exports = {
networks: {
development: {
host: "127.0.0.0",
port: 8545,
network_id: "*" // match any network
}
}
};


I was facing the same issue. What I did is

1. First install Ganache {goto setting change 7545 to 8546} also change truffle.js port to 8545.
2. Then open Meta-Mask and click on Localhost 8545.

Then just use the below commands:

truffle.cmd compile
truffle.cmd migrate
npm run dev


I had a problem where my networks section was empty within my contract file in the build directory like @rana_stack metioned. I used the solution here to fix it, making my migration run asynchronously and wait for the contract to deploy before writing to the json.

My migration file looked like this:

var MyContract = artifacts.require("./MyContract.sol");
const fs = require('fs');

module.exports = async (deployer, network, accounts) => {
await deployer.deploy(MyContract);

const metaDataFile = \${__dirname}/../build/contracts/MyContract.json

In my case, it's that I need to change the MetaMask to connect to a private network(URL) by choosing custom RPC and type in my URL http://127.0.0.1:9545 mannually