npm Commands for npm Noobs

Jul 5, 2020 · 2 mins read
npm Commands for npm Noobs

Since diving deeply into frontend development, I was having trouble adjusting with the node ecosystem, more specifically to the npm commands. I just keep on forgetting what the different npm commands do. So here’s my short notes on them.

Basics

$ npm --version              # check npm version
$ npm install -g npm@latest  # update npm

Notes:

  • npm packages can be installed locally or globally.
  • global packages are installed with -g flag or - -global flag
  • all installed packages get stored in node_modules folder

Package installation

# initialize a project that will contain node dependencies
$ npm init                    

# search for packages
$ npm search package_name

# install express package
$ npm install express    

# save package as a developer dependency (e.g. testing frameworks)
$ npm install express --save-dev 

# install a specific version of express
$ npm install express@1.2.3   

# uninstall express package
$ npm uninstall express       

# check installed packages
$ npm list --depth=0          # do with -g for global packages

Notes:

  • npm init generates package.json file. Subsequent operations may generate a package-lock.json file
    • package.json tracks installed packages
    • package-lock.json ensures same dependencies are used across all machines

A cloned git repo will not have dependencies installed. But a package.json will be present. To install the dependencies from package.json run

$ npm install   # make sure you are in project directory

Updating packages

# check for outdated packages
$ npm outdated    

# ~/.npm directory will get cluttered with old packages over time
# so clean it occasionally with
$ npm cache clean --force

# search for vulnerabilities in installed packages
$ npm audit

# updates packages to remove known vulnerabilities
$ npm audit fix

npx

Whereas npm is a tool for managing packages, npx is a tool for executing packages.

# For example
npx http-server

Sharing is caring!