Categories
Coding

How to Install npm With Yarn

Yarn is an alternative package manager to npm but it doesn’t replace npm entirely. Instead Yarn uses the npm registry.

Install Yarn

First you need to install Yarn. You can do this using npm.

npm install -g yarn

Use Yarn Commands

Once Yarn is installed you can use Yarn commands instead of npm commands. For example:

To install all dependencies:

yarn install

To add a new package:

yarn add [package-name]

To remove a package:

yarn remove [package-name]

Using npm Packages With Yarn

Yarn uses the same package.json file and npm registry so you can use npm packages with Yarn without any new config.

Converting From npm to Yarn

If you have a project using npm you can convert it to use Yarn:

  • Delete the node_modules folder
  • Delete package-lock.json (if it exists)
  • Run yarn install

yarn.lock File

Yarn will generate a yarn.lock file that is similar to package-lock.json. This guarantees consistent installs in multiple machines.

Global Packages

To install packages globally with Yarn

yarn global add [package-name]

Scripts

In your package.json, you can still use npm scripts. Yarn will use these as is.

"scripts": {
"start": "node index.js"
}

Run with: yarn start

Yarn doesn’t install npm itself. It’s an alternative client that uses the npm registry. If you need to use npm commands specifically, you’ll still need npm installed on your system.

Leave a Reply

Your email address will not be published. Required fields are marked *