133

I am using Yarn to install the dependencies of my project. What is the equivalent of "npm install <package_name> --save " in Yarn to update the entry in my package.json file? I can use "npm install <package_name> --save " here, but I want to use Yarn as much as possible to improve performance and avoid confusion between npm and Yarn.

3 Answers 3

157

The yarn equivalent tonpm install <name> --save is:

yarn add <name>

Here's the link to the docs for the full list of commands in comparison to npm.

5
  • 3
    Notice that the documentation (erroneously) states that NPM counterpart is npm install [package], while it's npm install --save [package] in fact. Sep 15, 2018 at 12:06
  • 9
    @estus the --save flag is not required anymore when using npm. If you don't want npm to save we now have to use npm --no-save :)
    – galdin
    Sep 15, 2018 at 14:02
  • 1
    Thanks for reminding. I have save option disabled globally in order for NPM to be consistent between versions. Sep 16, 2018 at 1:55
  • 6
    yarn add does not appear to update package.json by default Jan 29, 2021 at 15:24
  • @toastyghost I've just migrated from Yarn 1.x to 3.x and never esperienced a package not being added/removed from pakage.json using yarn add|remove I'm recommending the use of LTS versions. Try deleting node_modules and clean the cache then reinstall, there might be another issue regarding to this link: stackoverflow.com/questions/48654681/…
    – faebster
    Feb 27 at 23:16
75

Use the following command :

yarn add [package_name]

Comparing npm and Yarn Commands

Install dependencies

npm install => yarn 

Install a package

npm install [package_name] => yarn add [package_name]

Install a package globally

npm install -g [package_name] => yarn global add [package_name]

Install a package as a development dependency

npm install --save-dev [package_name] => yarn add --dev [package_name]

Uninstall a package

npm uninstall [package_name] => yarn remove [package_name]

Uninstall a package globally

npm uninstall -g [package_name] => yarn global remove [package_name]

Uninstall a development dependency package

npm uninstall --save-dev [package_name] => yarn remove [package_name]

Update the dependencies

npm update => yarn upgrade 

Update a package

npm update [package_name] => yarn upgrade [package_name]

Create a new package

npm init => yarn init

Run a script defined in the package.json

npm run => yarn run

Test a package

npm test => yarn test

Publish a package

npm publish => yarn publish

Remove all data from the cache

npm cache clean => yarn cache clean
1
  • 1
    Thanks for this cheat sheet. I keep referring to this comment whenever I need :)
    – Ikdemm
    Feb 10 at 9:57
51

Using --dev or -D will install one or more packages in your devDependencies.

yarn add <package...> [--dev/-D]

Yarn add documentation

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.