Even the smallest project will interact with at the very least two blockchain nodes: One on the developer’s machine, like Ganache or Truffle Develop, and the other representing the network where the developer will eventually deploy their application (such as the main public Ethereum network or a private consortium network, for instance). Truffle provides a system for managing the compilation and deployment artifacts for each network, and does so in a way that simplifies final application deployment.
See the Configuration section for more information.
Most Truffle commands will behave differently based on the network specified, and will use that network’s contracts and configuration. You can specify a network using the
--network option, like below:
$ truffle migrate --network live
In this example, Truffle will run your migrations on the “live” network, which – if configured like the example – is associated with the public Ethereum blockchain.
As mentioned in the Compiling contracts section, build artifacts are stored in the
./build/contracts directory as
.json files. When you compile your contracts or run your migrations using a specific network, Truffle will update those
.json files so they contain the information related to that network. When those artifacts are used later – such as within your frontend or application via truffle-contract – they’ll automatically detect which network the Ethereum client is connected to and use the correct contract artifacts accordingly.
Because the network is auto-detected by the contract artifacts at runtime, this means that you only need to deploy your application or frontend once. When you run your application, the running Ethereum client will determine which artifacts are used, and this will make your application very flexible. As an example, if you were to deploy a web application to http://mydapp.io, you could navigate to that address using your favorite wallet-browser (like MetaMask, or Mist) and your dapp would work correctly regardless of the Ethereum network the wallet-browser was connected to. If the wallet-browser was connected to the live network, your dapp would use the contracts you deployed on the live network. If on Ropsten, the contracts you deployed to Ropsten would be used.