Skip to content

Latest commit

 

History

History
242 lines (151 loc) · 5.88 KB

npm-tools.md

File metadata and controls

242 lines (151 loc) · 5.88 KB
title description keywords author marp theme _header _footer
NPM Tools
A short introduction to a collection of useful node packages for everyday use.
npm,tool,package,version
Marcel Eichner
true
marceleichner
2022-03-16

🧰 NPM Tools

A short introduction to a collection of useful node packages for everyday use.


⚒️ nvm

nvm is a version manager for node.js


Why?

nvm allows you to quickly install and use different versions of node via the command line on all major operating systems

  • POSIX-compliant shell: sh, dash, ksh, zsh, bash
  • platforms: unix, macOS, and windows WSL
  • simply install/uninstall node version(s)
  • have multiple node versions installed
  • pin node version used in project by adding a .nvmrc

🐕 husky

"modern native git hooks made easy" – Easily setup local git commit hooks for the whole team.


git hooks?

  • Run custom script(s) every time an action occurs (f.e. "commit", "push")
  • use cases:
    • lint file(s)
    • auto-format file(s)
    • run test(s)
    • build files
    • generate types
    • validate format of commit-message

Setup

Install the dependency:

npm install husky --save-dev;

Set the "prepare" script in package.json, install git-hooks:

npm set-script prepare "husky install"
npm run prepare

This will also create .husky directory and add it to .gitignore.


Example: pre-push

Run a script everytime new code is pushed to a remote with the pre-push action:

npx husky add .husky/pre-push "npm lint"
git add .husky/pre-push
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

# <insert lint command here>

bypass hooks

Sometimes scripts are slow and must be bypassed:

git commit --no-verify -m "chore: my message"

⬆️ npm-check

"Check for outdated, incorrect, and unused dependencies." - Interactive way of updating npm packages


Why?

  • get an overview of the up-to-date "state" of dependencies
  • visually appealing, F+STRG simple way of updating multiple packages
  • see different types of updates: patch, minor, major
  • find extranous packages
  • find missing packages

bg h:90%


"upgrades your package.json dependencies to the latest versions" - ignoring specified versions


Non-interactive - but more features!

bg right height:40%

  • ignore major (breaking) and minor updates: npx npm-check-update --target minor
  • run tests while updating npx npm-check-update --doctor

Analyze dependencies in a project and find unused and useless dependencies.


Use-Cases

  • ideal for integration into CI
  • detect packages that have been added by mistake
  • detect packages which are not actually used (anymore)

npx depcheck

bg right:70%


False Positive(s)

Some dependencies are not required in the sources but are used for compilation, linting and other tasks. Those packages often are detected and are false-positives and can be ignore while also documenting why they are added:

# .depcheck.yml
# use `npx depcheck --config .depcheck.yml`
ignores:
  # required by ngx-bootstrap
  - "bootstrap"
  # used for git hooks
  - "husky"

"Remove unwanted files and directories from your node_modules folder"


Why?

Collection of different glob-patterns & filters to remove files from node_modules which are not used.

  • reduce size of packaged applications (electron)
  • reduce size of docker image
  • in CI reduce space used for cache(s)
  • save space on machine
  • Example Benchmarks

height:300

"Easily find and remove old and heavy node_modules folders ✨"


bg h:78%


"Ever needed to see all the license info for a module and its dependencies?"


Why?

  • extract SPDX identifiers of packages

  • list licenses for legal auditing, documentation:

      npx license-checker-rseidelsohn --csv --out /path/to/licenses.csv
    
  • check if packages added with unwanted licenses

      npx license-checker-rseidelsohn --failOn 'GPL'
    

Others?


Thanks for listening!

bg left 50%