All of your contracts are located in the ./contracts directory. By default you're given an example Solidity contract file and an example Solidity library file, both with filenames ending in .sol. Although a Solidity library is different than a contract, for documentation purposes we're going to lump these into the same word, "contracts".


To compile your contracts, simply run:

$ truffle compile

Truffle will compile only the contracts that have been changed since the last compile, to reduce any unnecessary compilation. If you'd like to override this behavior, run the above command with the --all option.


Artifacts of your compilation will be placed in the ./build/contracts directory, relative to your project. This directory will be created if it does not exist. These artifacts are integral to the inner workings of Truffle, and they play an important part in the successful deployment of your application. You should not edit these files by hand as they'll be overwritten by contract compilation and deployment.


You can declare contract dependencies using Solidity's import command. Truffle will compile contracts in the correct order and ensure all dependencies are sent to the compiler. Dependencies can be specified in two ways:

 Importing dependencies via their file name:

To import contracts from a separate file, simply write the following command, where AnotherContract.sol is relative to the current contract being written. This will make all contracts within AnotherContract.sol available to the current source file.

import "./AnotherContract.sol";

Note that Solidity allows other import syntaxes as well. See their import documentation for more information.

 Importing contracts from an external package

Truffle supports dependencies installed via NPM as well as EthPM. To import contracts from a dependency, use the following syntax, where somepackage represents a package installed via NPM or EthPM, and /SomeContract.sol represents a path to a Solidity source file provided by that package.

import "somepackage/SomeContract.sol";

Note that Truffle will search installed packages from EthPM first before searching for packages installed from NPM, so in the rare case of a naming conflict the package installed via EthPM will be used.

For more information on how to use Truffle's package management features, please see Truffle's NPM and EthPM documentation, respectively.