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

To install Truffle you need to have Node and NPM along with Python setup on your machine.

If you do not have them already, you can install them from their official websites (Node and Python). Once you're done with that, you are all set to install Truffle.

We will use npm to install Truffle. Enter the following command in your command prompt:

npm install -g truffle

Screenshot-2022-07-16-190328

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:

Screenshot-2022-07-16-190639

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:

truffle version

in your command prompt. You should see an output like the image below:

Screenshot-2022-07-17-201823

Congratulations! You have installed Truffle.

ADVERTISEMENT

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.

Screenshot-2022-07-16-191633

In the marketplace search bar, type "Truffle for VS Code" and click on install (similar to the image below).

Screenshot-2022-07-16-191836

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:
Untitled

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.

Screenshot-2022-07-16-195911

From the dropdown box select 'Ganache service'.

Screenshot-2022-07-16-195943

Select type 'local' or 'fork'. Since this a local setup I will select 'local'.

Screenshot-2022-07-16-200019

Next, you will be asked to enter your 'local project's name'. Enter any name of your choice and hit enter.

Screenshot-2022-07-16-200046

Your network setup is complete.

Screenshot-2022-07-16-200242

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:
Screenshot-2022-07-16-200504

The output displays a set of things to speed up. We do not need to worry about them just yet.

ADVERTISEMENT

How to Start a Tuffle Project

To start a project in Truffle, go into a directory and type the init command.

truffle init

Screenshot-2022-07-16-193451

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>

Screenshot-2022-07-16-202855

Here we created a contract named 'SimpleStorage'.

ADVERTISEMENT

How to Run Tests in Truffle

To run test in Truffle, just enter this command:

truffle test

Screenshot-2022-07-17-171254

All the tests will be run one by one.

How to Deploy in Truffle

We will use Ganache to deploy in Truffle.

truffle develop

You use the above command to start the deployment process.

Ganache has some accounts and private key ready for this purpose.

Screenshot-2022-07-17-171755

In the above image at the bottom you will see truffle (develop)> console.

Screenshot-2022-07-17-172106

Type migrate --reset in the console.

Screenshot-2022-07-17-172204

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.

ADVERTISEMENT

Conclusion

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!