Understanding dependencies in Yarn packages
The packages dependencies defined by a package are very critical to the success of a package.
In this tutorial we will be looking at what dependencies are, as well as the types of dependencies that can exist in a package.
Dependencies and versions
The dependencies for a project are sets of existing codes that are defined in other packages which your project needs to function. They are listed in the package.json file. Every dependency in your package must have a minimum version that it requires.
The yarn.lock file makes sure that your package is consistent across installations by storing the version of the dependencies you install.
Types of dependencies
In a package, there are different types of dependencies which exists for different purposes. Some dependencies are required during development; some others are required to run the program. Some of the dependencies that you can have are (dependencies, devDependencies, and peerDependencies).
The package.json file for your project will contain all these dependencies:
{
"name": "my-project",
"dependencies": {
"package-a": "^1.0.0"
},
"devDependencies": {
"package-b": "^1.2.1"
},
"peerDependencies": {
"package-c": "^2.5.4"
},
"optionalDependencies": {
"package-d": "^3.1.0"
}
}
Most projects only have the dependencies and devDependencies, however, it is also important to understand each of these types of dependencies:
dependencies
This dependency is the most common, it is need for your code to run.
devDependencies
The devDependencies are your development dependencies. They are useful at some point in your development workflow but you don't need them when you are running your code (e.g. Babel or Flow).
peerDependencies
The peer dependencies are a special type of dependency that would you only use when you are publishing your own package.
Having a peer dependency indicates that there is a dependency that your package needs which is the same exact dependency as the person installing your package.
optionalDependencies
As the name suggests optional dependencies are optional. Yarn will still say the install process was successful even when they fail to install. You should consider using this when you have dependencies that might not work for certain machines (e.g watchman).
bundledDependencies
The bundledDependencies of your package defines an array of package names that are bundled whenever publishing your package.
The bundled dependencies should be inside your project. The functionality of the bundledDependencies is basically the same as normal dependencies. They will also be packed when you run yarn pack.
The normal dependencies are usually installed from the npm registry. Bundled dependencies are useful in cases where normal dependencies are not sufficient, these cases include:
- When you intend to re-use a third party library that does not come from the npm registry or that was modified.
- when you wish to re-use your own projects as modules.
- When there is need to distribute some files with your module.
Previous:
How to Publish your Yarn Package to the npm Registry.
Next:
Understanding Versions and dependencies in Yarn.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/yarn/dependencies.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics