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.
How to Install Truffle
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.
How to Use Truffle in Visual Studio Code
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.
How to Set Up Ganache in 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.
How to Start a Tuffle Project
To start a project in Truffle, go into a directory and type the init command.
This will create a new Truffle project.
How to Create a Contract in Truffle
The following commands creates a contract in Truffle:
truffle create contract <contract name>
Here we created a contract named ‘SimpleStorage’.
How to Run Tests in Truffle
To run test in Truffle, just enter this command:
All the tests will be run one by one.
How to Deploy in Truffle
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!
This content was originally published here.