I prefer to use npx over npm when possible so the commands can be executed without installing dependencies.

How to count the number of lines of code in a git repository?

In some situations you may need to know how many lines of code exist in a project, you can use the following command:

git ls-files | xargs wc -l

How to find CRLF line separators in a git repository?

In some situations you may need to locate files which have usually been created on a Windows machine which contain the CRLF character.

git grep -Il \$'\r'

How to find a package which contains a specific dependency?

For example, you may find an error during a build process with a specific package/module, but perhaps you can not locate where it exists. Usually, it's a dependency of a dependency. You can search the entire node_modules folder for a package name (Insert <package_name>):

find ./node_modules/ -name package.json | xargs grep <package_name>

How to kill a process running on a specific port?

For example, you are running a server on port 8000, you could kill that process with the following:

sudo lsof -t -i tcp:8000 | xargs kill -9

How to kill a process using part of the starting command?

For example, you are running webpack-dev-server, you could kill that process with the following:

kill -9 $(ps aux | grep 'webpack' | awk '{print $2}')

How to list the globally installed npm packages?

Generate a list of all of the globally installed packages on your machine:

npm list -g --depth 0

How to upgrade dependencies in the package.json file?

Upgrading node_modules can be exhausting. These quick commands can help:

  • npm update to perform safe dependency upgrades.
  • npm outdated to discover all of the outdated dependencies.
  • npm install packagename@latest to upgrade a package to the latest major version.
  • npx npm-check-updates -u to update the version numbers in the package.json file automatically. After updating the package.json file run npm install to upgrade all dependencies to their latest versions.

How to visualize the size of modules from a webpack bundle or selection of JavaScript files?

Pass a single webpack entry file and output the results as HTML:

npx source-map-explorer public/app-57b30d978d142b12cdd0.js --html result.html

Pass all the JavaScript files in a directory and output the results as HTML:

npx source-map-explorer public/*.js --html result.html

That's a Wrap!

I hope you have enjoyed this post and discovered some useful productivity hacks. I love to share and learn from the community if you have a favourite command-line productivity hack post it below!