When you’re working on a project and see a yarn.lock
file staring back at you, you might wonder, "What is yarn?". So let's begin.
Yarn is one of the main JS package managers that used for managing dependencies (packages and libraries) in JS projects.
Package manager is a tool that helps developers handle external libraries and modules required in project → automating the process of installing, updating and managing dependencies.
Installing yarn as your package manager you can do it with command: npm install -g yarn
(-g means globally on your machine) and after installation you can simply run yarn —version
to verify your installation of yarn.
Yarn works with package.json
.
package.json
file is a central configuration file in Node.js projects. It serves as a blueprint for the project and contains important metadata about the project, including its dependencies, scripts, versions, author information and more.
Yarn create a lock files yarn.lock
that's capture the exact version of all packages and their dependencies, providing a setup each time you install packages.
Package managers also run scripts for build, test and deploy.
Adding package with yarn yarn add package-name
this fetches the package from the registry and installs it locally in node modules folder. (https://classic.yarnpkg.com/en/docs/cli/add - all you need when work with adding packages)
If want to understand how yarn work you first need to understand what is Node.js.
Node.js is runtime environment that allows you to run the JS on a server. [JS → runtime → server].
In 1990's when JS is mostly used like scripting language to tun in the browser, as the web platforms evolved JS is became more powerful and 2009. Node.js is released.
Till then is impossible to run JS code on the server. Node.js bring for developer to be full stack and write whole application in one language that will run on server.
How it works?
When you visit a URL on the internet that points to your server when the request is received we can use node to handle request and read a file from the server’s file system and response back to the client so they can view the HTML in browser.
We said that Yarn is a package manager that work with Node.js to manage project dependencies (libraries or packages). When you developing Node.js project, you often need third-party libraries to perform various tasks (HTTP, work with DB, building UI..). Yarn is here to automatize the process of downloading, installing, updating and managing these libraries for us.
When you create project with yarn you initialize a package.json
file which defines the dependencies for your project. When you run commands like yarn install or yarn add, Yarn communicate with node.js packages, downloading them and places them in node_modules
folder within your project.
Node.js use CommonJS module system, where each file in Node.js is treated as a separated module. Allowing you to “require” modules into your code. With “require” it is trying to locate the express module by searching through a defined path starting with node_modules
.
Why is this important?
Node.js does not inherently manage dependencies! → it relies on external packages manager like Yarn to do this.
Finish conclusion!
Yarn helps Node.js with node_modules installing dependencijes that Node.js requires. Create yarn.lock to ensure consistent depencencies on various enviroments! Caches packages for faster install and offline support! Optimizes the structure of node_modules, making it more efficient for Node.js to search for dependencies. Handle dependencies conflicts and automates tasks with scripts, supporting Node.js application development and deployment.