Working with Yarn Package Manager
Why Yarn Package Manager?
- Do you have many npm modules that should be installed in your application?
- Is it taking much time to download all the necessary modules?
- What about having all the npm modules install in parallel?
- Yarn does this perfectly and thus it is too fast.
It Works offline Because, the modules are installed in a local cache, even though we are not connected to the internet, the yarn can install the modules.
Suppose delete the node_modules folder and rerun the yarn install command again. This time it will take less time than previous.
For the first time, when packages are installed, they will be saved in a local cache. So even after the folder is deleted and yarn install is run, the modules are installed from the local cache.
Same code base is failing in others machine? Maybe version differences of packages?
Using npm, If I do
npm install react, It adds “react”: “^15.5.4” to package.json. Which means, if there is a newer minor version of react package, it will download that version (note the ^ symbol which is added by default when using ‘npm install react’).
So if the same is installed using yarn command, it installs exactly same version all the time. A yarn lock file is created which contains which version of the package should be installed and also checksum for security purposes. This lock file helps to install exactly the same version of a package all the times and thereby avoiding unexpected errors due to package version differences.
The same can be achieved through npm using shrinkwrap. This is an extra step that should be done which yarn handles it internally.
Enough brief amount of theory to understand why Yarn, now let’s Start working with Yarn
Before you can start using Yarn, you should first have it installed on your machine.
On Windows, we can install it by running the MSI file from the below path.
You can cross check whether the yarn is installed or not from command prompt using ‘yarn –version’ command.
If yarn is successfully installed, it will display the installed version as below
Using Yarn, a new project can be created as below
yarn init command creates a package.json file. Running the command, below details should be entered.
If nothing is given, it takes default values present in the parenthesis as in above screenshot.
Once all the details are entered, a package.json is created in the folder.
Dependent packages can be added using yarn add [packagename] command.
For example, react can be added through yarn using the below command
yarn add react
It installs all the dependent modules required for react.
Updating a package can be done using yarn upgrade [packagename] command.
For example, react can be updated through yarn using the below command
yarn upgrade react
Uninstalling a package can be done using yarn remove [packagename] command
For example, react can be uninstalled through yarn using the below command
yarn remove react
Will post more articles to understand advanced commands in Yarn very soon.
Hope this article given you why and basic information on Yarn Package Manager.
Happy Coding . 🙂