npm and yarn

For Semantic Version is described elsewhere.

yarn

Quick Start

Documentation: https://yarnpkg.com/en/docs/

yarn init Spin up a new repo.

yarn install Install all packages listed in the package.json file.

yarn add -D packageName Downloads the package and save in devDependencies

yarn remove -D packageName Uninstalls the package and removes from package.json file.

npm - Node Package Manager

npm makes it easy for JavaScript developers to share and reuse code, and it makes it easy to update the code that you’re sharing.

Documentation

https://docs.npmjs.com/

Packages

Packages are just a folder containing reusable code. It will contain a package.json file.

Commands

There are a lot of npm commands, but these are the most common a site developer will need.

Install

Note: Before installing a package, it is a good idea to make sure npm itself is current. Issue:

npm update -g npm

npm install packageName

This will create the node_modules directory (if one doesn't exist yet), and will download the package to that directory when installing to a project. i.e. npm install package If you are installing globally (npm install -g package) then the package will probably be installed to /usr/local/lib/node_modules. To find out for sure, run npm list -g --depth=0

Install your development tools globally. This is because if you need to run an npm package from the command line then it is recommended that you install it globally. Conversely, if you application uses require() to include the package, then install it locally.

Note that if any package is already installed and satisfies the SemVer rule in package.json, running npm install will not update it to the latest version, even if there is a newer one satisfying the same rule. In fact, no change is made to the package in this case, even if it was modified.

Thus, to reliably reinstall the latest versions of all packages satisfying the semver rules in package.json, delete the node_modules folder and run npm install.

When saving to the package.json file, npm will include the version number. You may want to double check that it specified it the way you want. Refer to the SemVer syntax if needed.

Update

Local packages

Run npm update in the same directory as the package.json file.

Global packages

Run npm update -g packageName

To find out which packages need to be updated, you can use npm outdated -g --depth=0.

Uninstall

Local packages

Run npm uninstall packageName

Global packages

npm uninstall -g packageName

Other Common Commands

Spinning Up a New Site

You will need to do this if installing packages such as Grunt.

  1. npm init
    This will create the package.json file needed by npm.
  2. npm install grunt --save-dev
    This will download Grunt to your site and update the package.json file.
  3. Add some Grunt packages such as:
    npm install --save-dev grunt-babel
    npm install --save-dev grunt-contrib-jshint