Basic NPM Commands
Published: 2022-05-15 21:49:36

After setting up n node.js development environment, you need to know some basic commands of node package manager npm. The followings are the most commonly used ones.

``npm`` stands for node package manager.

###Installing npm

npm comes with the node.js. You can install node.js from their official website https://nodejs.org/en/download/
After installing node, You can check the version of node and npm by running the following commands:

```sh
node -v
```
```sh
npm -v
```

###Flags

-S is the same as ``--save``, and ``-D`` is the same as ``--save-dev``.

```sh
npm init
```
Initializes the package.json file of a project.

Use the ``-y``  flag to skip the questions and to create a package.json using default values.

```sh
npm install
```
Installs the dependencies listed in package.json.

Shorthand: ``npm i``

```sh
npm install <package>
```
Installs a package from the npm registry.

Shorthand: ``npm i <package>``

```sh
npm install -g <package>
```
Installs a package globally on your machine.

```sh
npm install <package-name@version-number>
```
Install a specific package version rather than the most latest version.

```sh
npm uninstall <package>
```
Like the command itself suggests, it uninstalls packages that are already installed.

Shorthand: npm uninstall <package>

```sh
npm uninstall -g <package>
```
Uninstalls a global package from your machine.

```sh
npm outdated
```
List the outdated packages for upgrade

```sh
npm update <package>
```
Updates the specified package to the latest version available. If package is not specified, it updates every package.

```sh
npm cache verify
```
Check how many cache entries are available.

Shorthand: ``npm up <package>``

```sh
npm cache clean —force
```
Clears the npm cache

```sh
npm audit
```
Performs a security check for all the available project packages.

```sh
npm update -g <package>
```
Updates a global package to the latest version available.

```sh
npm list
```
Lists all the installed packages and their versions, along with their dependencies in a tree structure.

```sh
npm list depth <number>
```
To get packages of a certain depth.
The packages installed by you will be in the depth 0. Its dependencies will be in the depth 1 and further dependencies will be in the depth 2 and so on.

```sh
npm view <package> <version>
```
Shows available details about the specified package. If the version is not set, it defaults to the latest version.

```sh
npm run <script>
```
Executes the specified script, if found as a property of the script object in package.json.

```sh
npm help
```
npm CLI has built in help command

```sh
npm help <term>Flags
```

``-S`` is the same as ``--save``, and ``-D`` is the same as ``--save-dev``.

Tries to show the appropriate documentation page for the term provided.

```sh
npm <command> -h
```

To get help for a particular command.

```sh
npm help-search <command>
```

To search npm documentation for help.

```sh
npm ci
```

This installs packages from package-lock.json without updating the dependency tree.

```sh
npm rebuild
```
Rebuild packages

```sh
npm version major
```
Find a major version

```sh
npm version minor
```
Find a minor version

```sh
npm version patch
```
Find the patch version

###Conclusion

This list is not exhaustive. I welcome any criticism,advice and contribution involving this work in the comments below.