Node.js

Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine.

Installation

Using Homebrew

brew install node

Manage multiple Node Version with nvm

Download and install nvm by running:

curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.31.1/install.sh | bash

Then download Node and select your version by running:

source ~/.bashrc        # source your bashrc/zshrc to add nvm to PATH
command -v nvm          # check the nvm use message
nvm install node        # install most recent Node stable version
nvm ls                  # list installed Node version
nvm use node            # use stable as current version
nvm ls-remote           # list all the Node versions you can install
nvm alias default node  # set the installed stable version as the default Node

See the documentation for information.

Switch automatically the Node and NPM version

Make sure you have set the default alias for nvm - e.g.: nvm alias default 8.11.2

Add nvm-auto to your plugins in ~/.zshrc.

Add a call to the nvm_auto_switch function after NVM is initialized in your .zshrc file.

Source .zshrc in your current shell or restart your shell.

npm usage

To install a package:

To install a package and save it in your project's package.json file:

To see what's installed:

To find outdated packages:

To upgrade all or a particular package:

To uninstall a package:

Symlink a package folder:

Yarn

For project with a lot of dependencies, or particular dependencies, we should probably use yarn.

Homebrew

You can install Yarn through the Homebrew. This will also install Node.js if it is not already installed.

If you use nvm or similar, you should exclude installing Node.js so that nvm’s version of Node.js is used.

Path Setup

If you chose manual installation, the following steps will add Yarn to path variable and run it from anywhere.

Note: your profile may be in your .profile, .bash_profile, .bashrc, .zshrc, etc.

  1. Add this to your profile: export PATH="$PATH:/opt/yarn-[version]/bin" (the path may vary depending on where you extracted Yarn to)

  2. In the terminal, log in and log out for the changes to take effect

To have access to Yarn’s executables globally, you will need to set up the PATH environment variable in your terminal. To do this, add export PATH="$PATH:`yarn global bin`" to your profile.

Upgrade Yarn

Yarn will warn you if a new version is available. To upgrade Yarn, you can do so with Homebrew.

Test that Yarn is installed by running:

Problems? If you are unable to install Yarn with any of these installers, please search through GitHub for an existing issue or open a new one.

Search for an existing issue · Open a new issue

Usage

Now that you have Yarn installed, you can start using Yarn. Here are some of the most common commands you’ll need.

Starting a new project

Adding a dependency

Adding a dependency to different categories of dependencies

Add to devDependencies, peerDependencies, and optionalDependencies respectively:

Upgrading a dependency

Removing a dependency

Installing all the dependencies of project

or

Install globally some useful packages

Additional Reading

How do I use Yarn? There are basic workflows for both creating and consuming Yarn packages that will help you get productive quickly.

CLI CommandsYarn is executed through a rich set of commands allowing package installation, administration, publishing, and more.

Last updated