Home  >  Article  >  Web Front-end  >  How to install nodejs with yarn

How to install nodejs with yarn

PHPz
PHPzOriginal
2023-05-11 14:40:08503browse

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

  1. Open the official website of Node.js https://nodejs.org.
  2. Click to download. This will automatically download the latest version of Node.js for Windows.
  3. Install the downloaded file.
  4. Open a command prompt and enter the following command to check whether Node.js is installed successfully:
node -v

If you see a version number, it means Node.js has been installed successfully.

MacOS

  1. Open the Terminal application and Homebrew. If you don't have Homebrew installed, open a Terminal application and enter the following command:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
  1. Then enter the following command to install Node.js:
brew install node
  1. Open a terminal and enter the following command to check whether Node.js is installed successfully:
node -v

If you see a version number, it means Node.js has been installed successfully.

Linux

  1. Open a terminal and enter the following command:
sudo apt-get update
  1. Then enter the following command to install Node.js and npm:
sudo apt-get install nodejs npm
  1. Open a terminal and enter the following command to check whether Node.js is installed successfully:
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

  1. Open Yarn’s official website https://classic.yarnpkg.com/en/docs/install/#windows-stable.
  2. Download the .msi file of the Stable version, run and install.
  3. Open the command prompt and enter the following command to check whether Yarn is installed successfully:
yarn -v

If you see a version number, it means Yarn has been installed successfully.

MacOS

  1. Open the terminal and enter the following command:
brew install yarn
  1. Open the terminal and enter the following command to check whether Yarn is installed successfully:
yarn -v

If you see a version number, it means Yarn has been installed successfully.

Linux

  1. Open a terminal and enter the following command:
curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
  1. Then enter the following command:
echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
  1. Then enter the following command again:
sudo apt-get update && sudo apt-get install yarn
  1. Open a terminal and enter the following command to check whether Yarn is installed successfully:
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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn