-y
flag is a convenient option that automatically responds ‘yes’ to all the prompts presented by npm during the initialization of a new project. If you prefer to provide individual responses to these prompts, which typically include details such as the project’s name, description, repository URL, and others, you can simply omit the -y
flag.
When initiating a node.js project with the npm init
command, you’re preparing the project to use npm, Node’s default package manager. This command creates a package.json
file in your project directory, which stores important metadata about your project. This includes the name and version of your application, description, entry point, test command, git repository, keywords, author, and license.
Additionally, the package.json
file manages project dependencies by listing all Node modules/packages installed via npm install
, and it allows for the creation of scripts to automate tasks such as starting your application or running tests.
Here’s an example of what a package.json
file might look like after running npm init
:
/src
— houses all source code, the core of your project. It’s the main directory for development./contracts
— stores Ethereum smart contract (.sol) files; used for defining the rules of transactions within your blockchain project./lib
— contains library scripts. These scripts provide reusable functionalities and reduce code duplication./routes
— holds server route files. They map URLs to specific functions that handle HTTP requests and responses./utils
— stores utility scripts and helper functions. These generally simplify complex tasks and improve code readability./test
— houses testing scripts. These validate your code’s functionality and help maintain software quality..gitignore
— lists files/folders that git should ignore. Helps prevent unwanted files (e.g., temporary files, logs) from being versioned.package.json
— manages package dependencies and scripts. It’s a manifest file for node.js projects that provides project metadata.README.md
— contains project documentation. A good README explains what the project does, how to use it, and other pertinent information.async
and await
keywords in the code are used to handle asynchronous operations in a more readable and cleaner way. The function getBalance
is marked as async
, making it return a Promise
. Within this function, await
is used to pause execution until the web3.eth.getBalance(address, block)
Promise resolves, effectively waiting for the balance of an Ethereum address at a specific block to be retrieved from the blockchain. Once this value is obtained, it’s logged into the console. This makes the asynchronous code appear as though it’s synchronous, simplifying its structure and readability.
.travis.yml
file for a node.js project:
dotenv
can help manage environment variables.