-->

Basic usage of npm command line


I'm assuming that you have already installed the nodejs and npm. If not install nodejs and npm it.

Create a package with npm

  • npm init
  • When we run above command It will ask us a couple of questions about the package that we are going to create. After executing the command it will create a "package.json" file.
  • We use "init" command only if we want to create a new package.

Installing global packages with npm

  • npm install -g <package name>
    
  • we install a package globally only if we want to use it as a command line
  • For example I you want to create a react native app we install react-native-cli globally. It provides "react-native" command we use it to create a react native package.
  • If you are using ubuntu use the command "sudo npm install -g <package name>" Because it requires superuser permissions.

Installing local packages with npm

  • npm install <package name>
  • We use this command very frequently.
  • It will install the specified package locally.
  • After running the above command you can see package inside "node_modules" directory.
  • If you want to install the package as a dependent package then try the below command. It will save the package name in "package.json" file under dependencies.
  • npm install <package name> --save
  • If you want to install it as development dependent package then try the below command. It will save the package name in "package.json" file under devDependencies.
  • npm install <package name> --save-dev
  • we don't require the development dependent packages  in production.

Uninstall a local package

  • npm remove <package name> --save 

Uninstall a global package

  • npm remove -g <package name> 

Search for a package

  • npm search <package terms>

List all globally installed packages

  • npm -g list

List all  locally installed packages

  • npm list

Setup for already existed package

  • npm install
  • when you run above command it will install all packages(development packages and production) from "package.json" file.

Buy a product to Support me