Now you know, NPM – Node Package Manager

So, I always used to wonder in my early stages of using NodeJS, why I hear alot of npm whenever I hear about NodeJS? As I dig into the details of npm with NodeJS, I found that it is so important for every JavaScript programmer who works in groups.

But how does that help?  npm has been a time savior for many JavaScript programmers by not letting them to replicate their work, as it allows them to share a piece of code between them in the means of package. So, the piece of code that can be shared is called as package/module.

All the basic details you might have to know about NPM:

  1. The code can be shared with the team by writing the code in a file with .js extension, example: random_name.js
  2. Once a developer shares the package with his team, his teammates can also check for the new versions on the package.
  3. The meta data of the package can be included in a .json file, typically called as package.json file.
  4. A package can be installed locally or globally. Typically one installs globally (tags with -g to regular commands) when it is used outside the project you are currently in like command line. When you want to use it within your project then it should be installed locally and can be accessed in the file by mentioning in require(‘package_name’)
  5. One can uninstall a package from the node_modules at any time if the package is no longer needed.

What is package.json:

  • package.json is a file which can be used to install the packages with a command npm install. 
  • The attributes are name, version, dependencies, devDependencies, author, contributors and many other.
  • name: package name
  • version: package version (if ^ is included in front of it then it looks for equal or greater than the current version)
  • dependencies: any dependencies with the package can be added.
  • devDependencies: the dependencies which are useful only in develop and testing environments can be added here, like testing tools etc.
  • author: creator of the package.
  • Other way to add dependencies is by using –save in command line. Syntax would look like, npm install <package_name> –save

General npm commands:

  • npm ls: To see all the packages (-g for global packages)
  • npm init: To start a package.json file
  • npm update: To update all the packages
  • npm install <package-name>: To install a particular package (npm install -g <package-name> for global packages)
  • npm install: To install all the latest version of packages listed in the dependencies
  • npm install <package-name> –save: To add dependencies in command line
  • npm config set prefix ‘~/.xxx’ : To configure npm teh prefix (the directory)
  • npm get prefix:  To know the location where it has been stored.
  • npm outdated: To see all the version updates on the packages.
  • npm search <pakage_name> : To search a package
  • npm uninstall <package_name>: To uninstall a package (add –save to remove completely from the dependencies)