Useful npm and node commands I use to boost productivity
Tags: node, productivity
Published: 2023-05-28 19:43:56

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?](#how-to-count-the-number-of-lines-of-code-in-a-git-repository)
- [How to find CRLF line separators in a git repository?](#how-to-find-crlf-line-separators-in-a-git-repository)
- [How to find a package which contains a specific dependency?](#how-to-find-a-package-which-contains-a-specific-dependency)
- [How to kill a process running on a specific port?](#how-to-kill-a-process-running-on-a-specific-port)
- [How to kill a process using part of the starting command?](#how-to-kill-a-process-using-part-of-the-starting-command)
- [How to list the globally installed npm packages?](#how-to-list-the-globally-installed-npm-packages)
- [How to list all packages installed by homebrew?](#how-to-list-all-packages-installed-by-homebrew)
- [How to upgrade dependencies in the package.json file?](#how-to-upgrade-dependencies-in-the-packagejson-file)
- [How to visualize the size of modules from a webpack bundle or selection of JavaScript files?](#how-to-visualize-the-size-of-modules-from-a-webpack-bundle-or-selection-of-javascript-files)

## 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 list all packages installed by homebrew?

Running `brew leaves` shows you all top-level packages and the snippet below also outputs a small description for each package:

```
brew leaves | xargs -n1 brew desc
```

## 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!