npm commands’ cheat-sheet

Communities for node programming and npm packages are increasing exponentially day by day. While using the npm packages, many times we need to check what is the latest version on the repository and installed version of a package locally, what are their dependencies, license, etc. Everytime we have to google for the command since they are kinda similar and we don’t remember.

After having such experiences frequently, I thought of creating a post for the commands I need to use mostly so that I can find everything in just one page. Here they all are.

  1. Show specified package’s version in the registry

    > npm view @angular/material version
    6.4.1

  2. Show all the published versions of a package (include pre-releases)

    > npm view @angular/material@* version
    @angular/material@5.0.0 ‘5.0.0’
    @angular/material@5.0.1 ‘5.0.1’
    @angular/material@5.0.2 ‘5.0.2’
    @angular/material@5.0.3 ‘5.0.3’
    @angular/material@5.0.4 ‘5.0.4’
    @angular/material@5.1.0 ‘5.1.0’
    @angular/material@5.1.1 ‘5.1.1’
    @angular/material@5.2.0 ‘5.2.0’
    @angular/material@5.2.1 ‘5.2.1’
    @angular/material@5.2.2 ‘5.2.2’
    ……..
    @angular/material@6.3.3 ‘6.3.3’
    @angular/material@6.4.0 ‘6.4.0’
    @angular/material@6.4.1 ‘6.4.1’

  3. Show package.json for the package on the registry

    > npm view @angular/material
    — You’ll see the result as per this link’s package.json

  4. Show me any attribute of the package.json on the registry

    > npm view ‘@angular/material’ repository
    { type: ‘git’,url: ‘git+https://github.com/angular/material2.git’ }

    > npm view ‘@angular/material’ license
    MIT

    > npm view ‘@angular/material’ bugs.url
    https://github.com/angular/material2/issue

    > npm view <package-name>[@version] <attribute-name>

  5. Show all the installed packages with their dependencies’ tree
    use -g to see the globally installed packages. For locally installed packages, just say npm list

    > npm list -g
    C:\Users\<UserName>\AppData\Roaming\npm
    +– @angular/cli@1.7.3
    | +– @angular-devkit/build-optimizer@0.3.2
    | | +– loader-utils@1.1.0 deduped
    | | +– source-map@0.5.7
    | | +– typescript@2.6.2
    | | `– webpack-sources@1.1.0 deduped
    | +– @angular-devkit/core@0.3.2
    ………….

  6. Show all the installed packages without their dependencies

    > npm list -g –depth=0
    C:\Users\<UserName>\AppData\Roaming\npm
    +– @angular/cli@1.7.3
    +– UNMET PEER DEPENDENCY @angular/compiler@^5.0.0 || ^6.0.0-rc.0
    +– UNMET PEER DEPENDENCY @angular/compiler-cli@^5.0.0 || ^6.0.0-rc.0
    +– @nrwl/schematics@0.11.1
    +– angular-library-set@1.0.0-alpha.5
    +– ng-packagr@2.4.1
    +– UNMET PEER DEPENDENCY tsickle@>=0.25.5
    +– UNMET PEER DEPENDENCY tslib@^1.7.1
    +– UNMET PEER DEPENDENCY typescript@>=2.4.2
    `– typings@2.1.1

  7. Install the latest version of a package

    > npm install <package-name>@latest
    > npm i @angular/material@latest

  8. Install a specific version of a package

    > npm i @angular/material@6.4.1

Also, while creating a new npm package for an Angular library project with nrwl, everytime we need to run the commands to build the project and to create .tgz file for it. You can create a script inside your package.json file. Let’s go step by step.

  1. Create an entry inside your script section in your package.json to build your project and to generate dist.

    “step1_build”: “ng build –project=<project-name>”

  2. Create another entry to create .tgz file for the dist generated, to upload to npm repository (needs ng-packagr)

    “step2_pack”: “cd dist/folder-name && npm pack”

  3. Now create the last entry to call the above two commands.

    “package”: “npm run step1_build && npm run step2_pack”

Now, whenever you are ready to build another version of the same lib, just run npm run package command from the prompt and your package will be ready!

 

What are the other commands you need to use most often and you need to google every time? Do provide them in the comments section below, will surely include them in this list!