NPM Cheat Sheet
Tags: npm, webdev, beginners
Published: 2021-06-13 04:56:16

This cheat sheet includes commands which will help you while using NPM packages and modules.
Don't worry about remembering all these NPM commands, Take a look at this cheat sheet whenever required.:wink:
Now go ahead and create something awesome.:sunglasses:

**Table of Contents**

- [Get Version](#get-version)
- [Get Help](#get-help)
- [Create package.json](#create-packagejson)
- [Set Defaults](#set-defaults)
- [Get Defaults](#get-defaults)
- [Remove Defaults](#remove-defaults)
- [Install Packages](#install-packages)
- [Install Certain Version](#install-certain-version)
- [Update Packages](#update-packages)
- [Remove Packages](#remove-packages)
- [Move to Another Folder](#move-to-another-folder)
- [Find Root Folder](#find-root-folder)
- [List Packages](#list-packages)
- [NPM Scripts](#npm-scripts)
- [Package Version](#package-version)

## Get Version

```bash
npm -v

npm --version
```

## Get Help

```bash
npm

npm help
```

## Create package.json

```bash
npm init

# Below commands will create package.json file with default values
npm init -y

npm init --yes
```

## Set Defaults

```bash
npm config set init-author-name 'YOUR NAME'

npm config set init-license 'MIT'
```

## Get Defaults

```bash
npm config get init-author-name

npm config get init-license
```

## Remove Defaults

```bash
npm config delete init-author-name

npm config delete init-license
```

## Install Packages

Globally

```bash
npm install -g package-name
```

Production dependency

```bash
npm install --save package-name
```

Development dependency

```bash
npm install --save-dev package-name
```

## Install Certain Version

Globally

```bash
npm install -g package-name@package-version
```

Production dependency

```bash
npm install --save package-name@package-version
```

Development dependency

```bash
npm install --save-dev package-name@package-version
```

## Update Packages

Globally

```bash
npm update -g package-name
```

Production dependency

```bash
npm update --save package-name
```

Development dependency

```bash
npm update --save-dev package-name
```

## Remove Packages

Globally

```bash
npm uninstall -g package-name
```

Production dependency

```bash
npm uninstall --save package-name
```

Development dependency

```bash
npm uninstall --save-dev package-name
```

## Move to Another Folder

NPM stores installed packages inside `node_modules` folder.
Most people usually doesn't share `node_modules` folder along with their code, because you can easily install all the NPM packages they have used using below commands.

Install production and development dependencies.

```bash
npm install
```

Install production dependencies only.

```bash
npm install --production
```

## Find Root Folder

Globally

```bash
npm root -g
```

Locally

```bash
npm root
```

## List Packages

Globally

```bash
npm list -g

npm list -g --depth 0

npm list -g --depth 1
```

Locally

```bash
npm list

npm list --depth 0

npm list --depth 1
```

## NPM Scripts

Define scripts in `package.json` file.

```javascript
"scripts": {
  "start": "node index.js",
  "script-name":"command-to-run"
}
```

Run scripts

```bash
# Only "start" script will execute without run command
npm start

npm run script-name
```

## Package Version

Find below, What package version with different symbols (\*, ~, ^) represents in `package.json` file.

| Version         | Result                                                                   |
| --------------- | ------------------------------------------------------------------------ |
| "\*"            | Install package with latest version                                      |
| "4.17.3"        | Install package with exact version                                       |
| "~4.17.**`3`**" | Install package with latest patch update (Highlighted part gets updated) |
| "^4.**`17.3`**" | Install package with latest minor update (Highlighted part gets updated) |

Most Preferred

```javascript
"package-name": "^4.17.3"
```