🔧
Practical Guide on Using Yarn in JavaScript Projects
September 04, 2022
Hello everyone!
In this post, I will share a step-by-step guide on how to use Yarn in your JavaScript projects. Yarn is a popular package manager for JavaScript, designed to improve speed, reliability, and security when managing project dependencies. Let’s get started!
Step 1: Installing Yarn
- First, make sure you have Node.js installed on your computer. You can download and install it from the official Node.js website.
- Once Node.js is installed, open the terminal or command prompt and run the command
node -vto verify the successful installation. - Next, install Yarn by running the command
npm install -g yarn. This will install Yarn globally on your computer.
Step 2: Creating a New Project
- Open the terminal or command prompt and navigate to the directory where you want to create your JavaScript project.
- Run the command
yarn initto create a new project with initial configuration. You will be prompted to fill in some information like the project name, version, description, and more. - After providing the required information, Yarn will generate a
package.jsonfile that contains your project configuration.
Step 3: Managing Dependencies
- To add a new dependency to your project, run the command
yarn add dependency-name. Replace “dependency-name” with the package name you want to add. For example,yarn add axioswill install the Axios package into your project and update thepackage.jsonfile with the new dependency information. - If you want to add a dependency as a development dependency, run the command
yarn add dependency-name --dev. This is useful for dependencies only required during development, such as testing tools or bundling setups. - Yarn also supports removing dependencies with the command
yarn remove dependency-name. This will remove the package from your project and update thepackage.jsonaccordingly.
Step 4: Managing Scripts
- In the
package.jsonfile, you can add custom scripts to be executed using Yarn. These scripts can be used for specific tasks, such as running a development server or building the project. - Open the
package.jsonfile and find the"scripts"section. There, you can add a new script with the format"script-name": "command". For example,"start": "node index.js"creates astartscript that runs theindex.jsfile using Node.js. - Once you’ve added a script, you can run it with the command
yarn script-name. For example,yarn startwill execute thestartscript you added.
Step 5: Running the Project
After managing dependencies and adding the necessary scripts, it’s time to run your JavaScript project using Yarn.
- Open the terminal or command prompt, ensuring you are in the JavaScript project directory.
- To run a script you’ve added, use the command
yarn script-name. For example, if you have a script"start": "node index.js"in yourpackage.jsonfile, run the commandyarn start. This will execute the script and start your application or server accordingly. - If you want to run a script directly without mentioning
yarn, you can use the commandyarn run script-name. For example,yarn run start. - Additionally, you can use built-in scripts like
yarn installto install the dependencies defined in thepackage.jsonfile oryarn buildto build your project for production.
With the above steps, you can easily run your JavaScript project using Yarn!