Truffle 命令手册

This section will describe every command available in the Truffle application.

使用方法

All commands are in the following form:

truffle <command> [options]

Passing no arguments is equivalent to truffle help, which will display a list of all commands and then exit.

命令列表

build

Execute build pipeline (if configuration present) (弃用).

truffle build

编译合约 compile

Compile contract source files.

truffle compile [--list <filter>] [--all] [--network <name>] [--quiet]

This will only compile contracts that have changed since the last compile, unless otherwise specified.

Options:

  • --list <filter>: List all recent stable releases from solc-bin. If filter is specified then it will display only that type of release or docker tags. The filter parameter must be one of the following: prereleases, releases, latestRelease or docker.

  • --all: Compile all contracts instead of only the contracts changed since last compile.

  • --network <name>: Specify the network to use, saving artifacts specific to that network. Network name must exist in the configuration.

  • --quiet: Suppress all compilation output.

config

Display whether analytics are enabled or disabled and prompt whether to toggle the setting.

truffle config [--enable-analytics|--disable-analytics]

Options:

  • --enable-analytics|--disable-analytics: Enable or disable analytics.

console

Run a console with contract abstractions and commands available.

truffle console [--network <name>] [--verbose-rpc]

Spawns an interface to interact with contracts via the command line. Additionally, many Truffle commands are available within the console (without the truffle prefix).

Requires an external Ethereum client, such as Ganache or geth. For a console that creates a development and test environment, use truffle develop.

See the 使用控制台 section for more details.

Options:

  • --network <name>: Specify the network to use. Network name must exist in the configuration.

  • --verbose-rpc: Log communication between Truffle and the Ethereum client.

create

Helper to create new contracts, migrations and tests.

truffle create <artifact_type> <ArtifactName>

Options:

  • <artifact_type>: Create a new artifact where artifact_type is one of the following: contract, migration or test. The new artifact is created along with one of the following files: contracts/ArtifactName.sol, migrations/####_artifact_name.js or tests/artifact_name.js. (required)

  • <ArtifactName>: Name of new artifact. (required)

Camel case names of artifacts will be converted to underscore-separated file names for the migrations and tests. Number prefixes for migrations are automatically generated.

debug

Interactively debug any transaction on the blockchain.

truffle debug <transaction_hash>

Will start an interactive debugging session on a particular transaction. Allows you to step through each action and replay. See the 调试合约 section for more details.

**Alert**: This command is considered experimental.

Option:

  • <transaction_hash>: Transaction ID to use for debugging. (required)

deploy

Alias for migrate. See migrate for details.

develop

Open a console with a development blockchain

truffle develop

Spawns a local development blockchain, and allows you to interact with contracts via the command line. Additionally, many Truffle commands are available within the console (without the truffle prefix).

If you want an interactive console but want to use an existing blockchain, use truffle console.

See the Using the console section for more details.

exec

Execute a JS module within the Truffle environment.

truffle exec <script.js> [--network <name>] [--compile]

This will include web3, set the default provider based on the network specified (if any), and include your contracts as global objects while executing the script. Your script must export a function that Truffle can run.

See the Writing external scripts section for more details.

Options:

  • <script.js>: JavaScript file to be executed. Can include path information if the script does not exist in the current directory. (required)

  • --network <name>: Specify the network to use, using artifacts specific to that network. Network name must exist in the configuration.

  • --compile: Compile contracts before executing the script.

help

Display a list of all commands or information about a specific command.

truffle help [<command>]

Option:

  • <command>: Display usage information about the specified command.

init

Initialize new and empty Ethereum project

truffle init [--force]

Creates a new and empty Truffle project within the current working directory.

**Alert**: Older versions of Truffle used `truffle init bare` to create an empty project. This usage has been deprecated. Those looking for the MetaCoin example that used to be available through `truffle init` should use `truffle unbox MetaCoin` instead.

Option:

  • --force: Initialize project regardless of the current working directory’s state. Be careful, this could overwrite existing files that have name conflicts.

install

Install a package from the Ethereum Package Registry.

truffle install <package_name>[@<version>]

Options:

  • <package_name>: Name of the package as listed in the Ethereum Package Registry. (required)

  • @<version>: When specified, will install a specific version of the package, otherwise will install the latest version.

See the Package Management with EthPM section for more details.

migrate

Run migrations to deploy contracts.

truffle migrate [--reset] [--f <number>] [--to <number>] [--network <name>] [--compile-all] [--verbose-rpc] [--dry-run] [--interactive]

Unless specified, this will run from the last completed migration. See the (迁移)合约部署 section for more details.

Options:

  • --reset: Run all migrations from the beginning, instead of running from the last completed migration.

  • --f <number>: Run contracts from a specific migration. The number refers to the prefix of the migration file.

  • --to <number>: Run contracts to a specific migration. The number refers to the prefix of the migration file.

  • --network <name>: Specify the network to use, saving artifacts specific to that network. Network name must exist in the configuration.

  • --compile-all: Compile all contracts instead of intelligently choosing which contracts need to be compiled.

  • --verbose-rpc: Log communication between Truffle and the Ethereum client.

  • --dry-run: Fork the network specified and only perform a test migration.

  • --interactive: Prompt to confirm that the user wants to proceed after the dry run.

networks

Show addresses for deployed contracts on each network.

truffle networks [--clean]

Use this command before publishing your package to see if there are any extraneous network artifacts you don’t want published. With no options specified, this package will simply output the current artifact state.

Option:

  • --clean: Remove all network artifacts that aren’t associated with a named network.

opcode

Print the compiled opcodes for a given contract.

truffle opcode <contract_name>

Option:

  • <contract_name>: Name of the contract to print opcodes for. Must be a contract name, not a file name. (required)

publish

Publish a package to the Ethereum Package Registry.

truffle publish

All parameters are pulled from your project’s configuration file. Takes no arguments. See the Package Management with EthPM section for more details.

run

**Note**: This feature is new and still in a barebones state. Please let us know how we can improve it!

Run a third-party plugin command

truffle run <command>

Option:

  • <command>: Name of a command defined by an installed plugin. (required)

Install plugins as NPM package dependencies and configure Truffle to recognize the plugin. For more information, see Third-Party Plugin Commands.

serve

Serve the built app from http://127.0.0.1:8080, rebuilding and redeploying changes as needed. Similar to truffle watch, but with the web server component added.

truffle serve [-p <port>] [--network <name>]

Options:

  • -p <port>: Specify the port to serve on. Default is 8080.

  • --network <name>: Specify the network to use, using artifacts specific to that network. Network name must exist in the configuration.

**Alert**: This command is deprecated. Please use third-party development servers like [webpack-dev-server](https://github.com/webpack/webpack-dev-server) instead. See our [Webpack Truffle Box](https://truffleframework.com/boxes/webpack) for an example.

test

Run JavaScript and Solidity tests.

truffle test [<test_file>] [--compile-all] [--network <name>] [--verbose-rpc] [--show-events]

Runs some or all tests within the test/ directory as specified. See the section on Testing your contracts for more information.

Options:

  • <test_file>: Name of the test file to be run. Can include path information if the file does not exist in the current directory.

  • --compile-all: Compile all contracts instead of intelligently choosing which contracts need to be compiled.

  • --network <name>: Specify the network to use, using artifacts specific to that network. Network name must exist in the configuration.

  • --verbose-rpc: Log communication between Truffle and the Ethereum client.

  • --show-events: Log all contract events.

unbox

Download a Truffle Box, a pre-built Truffle project.

truffle unbox <box_name>

Downloads a Truffle Box to the current working directory. See the list of available boxes.

You can also design and create your own boxes! See the section on Truffle boxes for more information.

Options:

  • <box_name>: Name of the Truffle Box. (required)

  • --force: Unbox project in the current directory regardless of its state. Be careful, this will potentially overwrite files that exist in the directory.

version

Show version number and exit.

truffle version

watch

Watch filesystem for changes and rebuild the project automatically.

truffle watch

This command will initiate a watch for changes to contracts, application, and configuration files. When there’s a change, it will rebuild the app as necessary.

**Alert**: This command is deprecated. Please use external tools to watch for filesystem changes and rerun tests.