Learning a new technology often means learning a new framework, programming language, IDE, or deployment method. And the blockchain is no different.
In this tutorial, I am going to show you how to get started with Truffle, a Node.js blockchain framework, in Visual Studio Code.
We will use npm to install Truffle. Enter the following command in your command prompt:
npm install -g truffle
While the installation runs, if you come across the following error, I have got you covered:
gyp ERR! stack Error: Could not find any Visual Studio installation to use
Google shows a bunch of solutions for the above error. What actually worked for me was installing Visual Studio along with 'Desktop Development with C++'.
After you download Visual Studio and run the installer you will see the following screen:
Under the 'Desktop and mobile' section, check 'Desktop Developement with C++' and continue with the installation process.
Once this is done you can run the Truffle installation command again.
To verify if Truffle is installed successfully, run:
in your command prompt. You should see an output like the image below:
Congratulations! You have installed Truffle.
Visual Studio Code comes with it's own extension for Truffle. We will install it to make our work easier.
In the marketplace search bar, type "Truffle for VS Code" and click on install (similar to the image below).
VS Code requires you to have other extensions for it to work, so just check the ones you don't have installed and continue the setup:
If you do not see the Truffle logo in the left bar you might have to restart VS Code.
Ganache comes with the Truffle suite to deploy DApps.
Click on the 'Networks' > 'Create a new network' in Truffle explorer.
From the dropdown box select 'Ganache service'.
Select type 'local' or 'fork'. Since this a local setup I will select 'local'.
Next, you will be asked to enter your 'local project's name'. Enter any name of your choice and hit enter.
Your network setup is complete.
To start the network, right click on the network name and click on 'Start Ganache'.
When you start the Ganache service, you will see a command line output as follows:
The output displays a set of things to speed up. We do not need to worry about them just yet.
To start a project in Truffle, go into a directory and type the init command.
This will create a new Truffle project.
The following commands creates a contract in Truffle:
truffle create contract <contract name>
Here we created a contract named 'SimpleStorage'.
To run test in Truffle, just enter this command:
All the tests will be run one by one.
We will use Ganache to deploy in Truffle.
You use the above command to start the deployment process.
Ganache has some accounts and private key ready for this purpose.
In the above image at the bottom you will see
truffle (develop)> console.
migrate --reset in the console.
You will see that the initial migrations are made and the deployment process starts. In the end you will get a summary (the cost and number of deployments) of the deployment.
We progress more quickly when we can learn from each other's mistake. I have just started learning Blockchain, and it took me a while to get the setup running. So here I am sharing my findings. Happy learning!
Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs as developers. Get started