npm & yarn basics

npm & yarn basics

Introduction

npm vs yarn

Package manager is a tool responsible for automating processes related to working with packages. Software packages are located in archive files, and each package contains meta-data that describes the given software (such as the software name, version, and list of all dependencies).
The most common application of package manager is when installing packages and their dependencies. Package manager eliminates the need to manually install software. In addition, package manager can handle updating, configuring and finally removing all those components.

The biggest package manager is npm, which is primarily made for the node.js (server) environment, so in order for it to work in a browser, it is necessary to use module bundler (browserify or webpack). Yarn package manager is the “new kid in town”, Facebook’s baby, it can use all npm packages and has a very similar syntax but it is considered to execute commands faster than npm.

Dependency Organization

npm3 dependencies tree small

npm3 dependencies tree ×

npm uses a nested dependency tree. Nested dependency tree implies that for each package its dependencies are installed, so that the same dependencies (eg Jquery) can be loaded multiple times but for different programs. This type of organization requires more space but allows applications to use different versions of the same software in one project.

It should be noted that the latest version of npm (npm3) tries to prevent “dependencies hell” and its style is more and more like “flat way”. See more about dependencies tree in npm3 on their site.

Example

Installation and initialization

Package manager installation

The npm package manager installation is part of the node.js installation. So first you need node.js with official page where we choose the appropriate version of Windows Installer (.msi) and then do the standard installation.

Since Yarn is used mainly for npm packages, we need to have node.js installed before installing Yarn itself. Yarn installation is standard, you need to download the installation .msi file from the official website. You can download the download link here, followed by the standard installation with next…next…next

Update package manager

Updating “npm” with command:

Yarn update is done from npm

Initialization of the package manager in the project

Once we have the application installed on our operating system then we can use it in our project. When initializing the package manager, a “package.json” file is generated with the command:



Working with packages

Installing new packages

a) Global installation (operating system)


NOTE:
Unlike npm where the “–globall” flag is used, yarn uses the command!
COMMAND “global” IS WRITTEN IMMEDIATELY AFTER “yarn”!!!

b) Local installation (inproject)

Package installation is done with the command:

Installation options:

Flag Purpose Shortcut
–save Adding packages to Dependencies -S
–save-dev Adding packages to devDependencies -D
@1.2.3 An extension pasted to the package name that defines the exact version of the package we want to install (if omitted it installs the latest version) /

Installation Options

Flag Purpose Shortcut
–dev Adding packages to devDependencies -D
@1.2.3 An extension pasted to the package name that defines the exact version of the package we want to install (if omitted it installs the latest version) /

Installing all packages from package.json

When a project is cloned, it does not come with installed packages, but only with instructions on what to install (package.json). Installation of all dependencies defined in the package.json file is done with:

or

or simply

Update dependencies

Individual package updates

With the command to update the installed package, we need to use the appropriate flag that was used during installation:

Reinstall all packages



Removing dependencies

With the command to remove the installed package, we need to use the appropriate flag that was used during installation:

With yarn, it is not necessary to use the flag when uninstalling:

Starting the package

The corresponding package is started with the command:


Tips & tricks

Using Git repositories instead of npm Modules

If we don’t want to use the official site but some private repository, we can use the following code in package.json:

Deleting undeclared modules

If we ever forget to use --save or --save-dev when installing a package, use the command “prune” to delete such packages:

or in production you need to add –production:

This command deletes all packages that are not in the “package.json” file.

Defining package version

With adequate signs we can more accurately define the package version:

  • (^) targets all major versions eg. (1.*.*)
  • (~) targets the latest minor version, e.g. (1.2.*)
  • Exact version of e.g. (1.2.3)

Fixing package version

If we want to fix the version “forever” during the installation, it is enough to use:

Overview of outdated versions

This command lists the version status for each package at a glance: