NPM

NPM is the largest ecosystem of open source libraries in the world.

NPM stands for Node.js' package manager.

Make sure you have Node and NPM installed by running simple commands

to see what version of each is installed and to run a simple test program.

install NPM on windows

  1. Download the Windows installer from the [Nodes.js® web site.](https://nodejs.org/en/\)

  2. Run the installer (the .msi file you downloaded in the previous step.)

  3. Follow the prompts in the installer (Accept the license agreement, click the NEXT button a bunch of times and accept the default installation settings).

installer

  1. Restart your computer. You won’t be able to run Node.js® until you restart your computer.

Command line interface (aka CLI)

The npm command-line tool is bundled with Node.js.

npm also has a command line client that allows developers to install and publish your packages.

npm --version
 2.14.12

The second important command is npm init:

$ npm init
package name: (project)
version: (1.0.0)
description: Demo of package.json
entry point: (index.js)
test command:
git repository:
keywords:
author:
license: (ISC)

Press Enter to accept the defaults, then type yes to confirm. This will create a package.json file at the root of the project.

{
  "name": "project",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC"
}

Tip: You can also issue the command for default initialization of all the defaults with

This way to generate a package.json file use npm init --y

NPM contains different APIs which will shown here:

You can use the following command in order to list down all the locally installed modules

npm ls

Search for packages in the repositry

npm search mocha

You can install modules with NPM locally or globally.

When you install modules globally they are installed in a system directory.

Where can you find all the packages?

https://npmjs.org

installing packages with npm

NPM comes along with Node.js

to install a packages simply run:

  • npm install package-name
  • npm install tarball file
  • npm install tarball url

If you want to install a package globally you can issue:

  • npm install -g package-name

Note: global installation is against best practices. because when you will deploy your app with CI to different servers these globally tools will not be there and will cause dependencies issues that are ahrd to overcome. the preferred way is to install this dependencies as local or if they are tools then you can put them in the bin folder.

Global installation makes the package available globally irrespective to the directory you installed it from.

Is you want to install as development dependency issue (shortcuts)

  • npm i -D

Is you want to install as a normal dependency issue (shortcuts)

  • npm i -S

package.json file

Undersatnding the pacage.json:

  • It is a valid JSON object

  • name and version fields are required, the combination makes a unique identifier for the package

  • There are Some used fields in package.json
    • description
    • keywords
    • homepage
    • bugs
    • license
    • author & contributers
    • main
  • the dependencies section is where you know which packages are inside this project build
  • The script section is where you define your script commands (start. preinstall)
  • The bin field is the folder where your binaries of this project exist
  1. this file also serves as documentation for what packages your project depends on

  2. It allows you to specify the versions of a packagefor your prohject according to semantic versioning rules](https://docs.npmjs.com/getting-started/semantic-versioning\). take a look in this video:

results matching ""

    No results matching ""