Yarn, a popular package manager for JavaScript, plays a vital role in managing and installing packages and dependencies. Understanding its commands is crucial for any JavaScript developer. This post lists 25 commonly used Yarn commands and provides code examples.
1. yarn add
Purpose: Installs a package from the npm registry or a local directory.
Syntax:
yarn add <package name>
Example:
$ yarn add express
2. yarn remove
Purpose: Uninstalls a package.
Syntax:
yarn remove <package name>
Example:
$ yarn remove express
3. yarn upgrade
Purpose: Upgrades all installed packages to their latest versions.
Syntax:
yarn upgrade
Example:
$ yarn upgrade
4. yarn outdated
Purpose: Lists outdated installed packages.
Syntax:
yarn outdated
Example:
$ yarn outdated
Package Current Wanted Latest
express 4.17.3 5.0.0 5.0.0
body-parser 1.19.1 2.0.0 2.0.0
5. yarn audit
Purpose: Checks for security vulnerabilities in installed packages.
Syntax:
yarn audit
Example:
$ yarn audit
6. yarn run
Purpose: Runs a script defined in the package.json file.
Syntax:
yarn run <script name>
Example:
$ yarn run start
7. yarn start
Purpose: Runs the default script specified in the package.json file, typically used to start the application.
Syntax:
yarn start
Example:
$ yarn start
8. yarn test
Purpose: Runs the test script specified in the package.json file, typically used to run unit tests.
Syntax:
yarn test
Example:
$ yarn test
9. yarn publish
Purpose: Publishes a package to the npm registry.
Syntax:
yarn publish
Example:
$ yarn publish
10. yarn link
Purpose: Creates a symlink between a local package and the global node_modules directory.
Syntax:
yarn link <package name>
Example:
$ yarn link my-package
11. yarn unlink
Purpose: Removes a symlink created by yarn link.
Syntax:
yarn unlink <package name>
Example:
$ yarn unlink my-package
12. yarn list
Purpose: Lists installed packages.
Syntax:
yarn list
Example:
$ yarn list
├─┬ express@5.0.0
│ └── accepts@~1.3.8
├─┬ body-parser@2.0.0
│ ├── bytes@3.1.2
│ ├── content-type@~1.0.4
│ ├── debug@2.6.9
...
13. yarn search
Purpose: Searches for packages on the npm registry.
Syntax:
yarn search <package name>
Example:
$ yarn search react
14. yarn view
Purpose: Displays information about a package, such as its description, author, and dependencies.
Syntax:
yarn view <package name>
Example:
$ yarn view express
15. yarn global add
Purpose: Installs a package globally, making it available to all projects on your system.
Syntax:
yarn global add <package name>
Example:
$ yarn global add create-react-app
16. yarn global remove
Purpose: Uninstalls a globally installed package.
Syntax:
yarn global remove <package name>
Example:
$ yarn global remove create-react-app
17. yarn config
Purpose: Gets or sets Yarn configuration options.
Syntax:
yarn config [<key>] [<value>]
Example:
$ yarn config get registry
https://registry.yarnpkg.com/
18. yarn cache clean
Purpose: Clears the Yarn cache.
Syntax:
yarn cache clean
Example:
$ yarn cache clean
19. yarn dedupe
Purpose: Removes duplicate packages and their dependencies from the node_modules directory.
Syntax:
yarn dedupe
Example:
$ yarn dedupe
20. yarn init
Purpose: Initializes a new Yarn project by creating a package.json file.
Syntax:
yarn init
Example:
$ yarn init
21. yarn install --check-files
Purpose: Checks if the installed packages match the versions specified in the package.json file.
Syntax:
yarn install --check-files
Example:
$ yarn install --check-files
22. yarn install --ignore-scripts
Purpose: Installs packages without running preinstall or postinstall scripts.
Syntax:
yarn install --ignore-scripts
Example:
$ yarn install --ignore-scripts
23. yarn install --offline
Purpose: Installs packages from the cache without connecting to the npm registry.
Syntax:
yarn install --offline
Example:
$ yarn install --offline
24. yarn berry
Purpose: Uses Yarn Berry, the next-generation version of Yarn.
Syntax:
yarn berry
Example:
$ yarn berry
25. yarn help
Purpose: Displays help information for a specific command or Yarn in general.
Syntax:
yarn help <command>
Example:
$ yarn help install
Conclusion
Mastering these 25 Yarn commands will empower you to effectively manage JavaScript packages and dependencies. By leveraging these commands, you can install and update packages, resolve vulnerabilities, publish your own packages, and streamline your development workflow. Remember to experiment with these commands and familiarize yourself with their options and capabilities.
Comments
Post a Comment
Oof!