NPM CHEAT SHEET πŸ‘©πŸ½β€πŸ’»

Aman
Catalysts Reachout
Published in
3 min readSep 12, 2022
photo from alamy.com

If someone did ever try react or css frameworks they would have come across npm(node package manager) to download the particular package. But, as your app grows your package also grows and maintaining that list of package is very important.

So, in this article, we are going to learn something about Npm commands to maintain the packages and what is really a package ?

What is the package?

To learn about package , first we have to know about module. Module is the reusable code in a file which comes in handy for the developers while executing the code. Package is the collection of modules stored in a directory along with a package.json file which contains the dependencies list of the project or application.

Some useful npm commands :- Npm __________

npm install

This will install all the dependencies list present in package.json important for the application .

npm install [package_name] --save or npm i[package_name] -S

This installs the given package locally and add it to your package json dependency list.

npm install [package_name] --global or npm i [package_name] -g

This installs the given package globally.

npm install [package_name] --save-dev or npm i [package_name] -D

This installs the given package as devDependency.

npm list --save

This will list out all the dependency list locally present in package.json.

npm list -g true --depth [0 | 1 | 2 | 3 | …]

This will list out the package list installed globally with its nested subpackages defined by the respective depth level.

npm outdated -S

This will show the list of local packages which are outdated.

npm outdated -g

This will show the list of global packages which are outdated.

Before jumping to the installing the latest version of the outdated package, let’s understand about that three numbers shown in dependency list separated by (.) .

  1. Major Number : this is the number for new changes that break backward compatibility
  2. Minor Number : this is the number for new features but does not break the compatibility
  3. Patch Number : this is the number for fixing bugs with the same compatibility.

Note :- To install the latest major release , replace the three numbers with a single β€˜*’ inside a double quotation marks and run npm install [package_name]

To install the latest minor number , use β€˜^’ in front of three numbers inside a double quotation marks and run npm install [package_name]

To install the latest patch number, use ~ in front of three numbers inside a double quotation marks and run npm install [package_name]

Hence, when you clone any project from Github and have a look on dependency list , they contain ^ in front of the numbers, it is due to have same compatibility with the code provided in the project so that code does not break .

To install the latest version of the package , simply we can run this code:-

npm install [package_name]@latest

last but not the least command of npm :-

npm prune

This will remove extraneous packages which are the package installed for the project like node js but not saved in the dependency list.

These all above listed commands can also be executed in the system terminal. So, feel free to do so.πŸ˜‡πŸ˜‡

Hope this article will help the learner in their learning path.✌✌

--

--