As the use of Node.js becomes more widespread, many developers are looking for faster and easier ways to manage their valuable Node.js applications. This is how Yarn was born. Yarn is a fast, reliable, and secure package manager primarily used for Node.js applications. In this article, we will cover how to install Node.js and use Yarn to manage your Node.js applications.
What is Node.js?
Node.js is a JavaScript runtime based on the Chrome V8 engine. It allows running JavaScript code on the server side and can also be used to develop command line tools and APIs. Node.js was originally created by Ryan Dahl in 2009 with the goal of making server-side JavaScript easier.
One of the core advantages of Node.js is that it can handle large numbers of concurrent connections very quickly, which makes it ideal for building web applications. Many famous companies such as Netflix, PayPal, Uber, LinkedIn, etc. are using Node.js to build their applications.
Install Node.js
Before you start using Yarn to manage your Node.js applications, you need to install Node.js. In this section, we will show you how to install Node.js on Windows, MacOS, and Linux.
Windows
node -v
If you see a version number, it means Node.js has been installed successfully.
MacOS
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
brew install node
node -v
If you see a version number, it means Node.js has been installed successfully.
Linux
sudo apt-get update
sudo apt-get install nodejs npm
node -v
If you see a version number, it means Node.js has been installed successfully: Installed successfully.
Install Yarn
Once you have successfully installed Node.js, the next step is to install Yarn. In this section, we will show you how to install Yarn on Windows, MacOS, and Linux.
Windows
yarn -v
If you see a version number, it means Yarn has been installed successfully.
MacOS
brew install yarn
yarn -v
If you see a version number, it means Yarn has been installed successfully.
Linux
curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
sudo apt-get update && sudo apt-get install yarn
yarn -v
If you see A version number indicates that Yarn has been successfully installed.
Use Yarn to manage dependencies
Now that you have installed Node.js and Yarn in your system, let’s learn how to use Yarn to manage your Node.js application. Dependencies. Yarn uses a file called package.json
to record the third-party packages that the application depends on. In this section, we will show you how to use Yarn to install, update, and uninstall packages.
Install the package
To install the package, open a terminal and go into your project directory. Then, use the following command to install the package:
yarn add package_name
For example, if you want to install the lodash package, you can use the following command:
yarn add lodash
This will install the lodash package to your project , and add it to your package.json
file.
Update package
To update the package, use the following command:
yarn upgrade package_name
For example, if you want to update the lodash package, you can use the following command:
yarn upgrade lodash
This will update the lodash package to the latest version.
Uninstall the package
To uninstall the package, use the following command:
yarn remove package_name
For example, if you want to uninstall the lodash package, you can use the following command:
yarn remove lodash
This will remove the lodash package and delete it from your package.json
file.
Conclusion
In this article, we covered how to use Yarn to manage the dependencies of your Node.js application. We showed you how to prepare your system by installing Node.js and Yarn, and showed you how to install, update, and uninstall packages. Now that you understand the basics of Yarn, try using it to simplify your application management on your next development project.
The above is the detailed content of How to install nodejs with yarn. For more information, please follow other related articles on the PHP Chinese website!