DEV Community

Michael Fasani
Michael Fasani

Posted on • Originally published at michaelfasani.com

Useful npm and node commands I use to boost productivity

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
Enter fullscreen mode Exit fullscreen mode

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'
Enter fullscreen mode Exit fullscreen mode

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 ):

find ./node_modules/ -name package.json | xargs grep <package_name>
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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}')
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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

npx source-map-explorer public/*.js --html result.html
Enter fullscreen mode Exit fullscreen mode

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!

Top comments (2)

Collapse
 
iamandrewluca profile image
Andrei Luca

Nice list!

For the "How to count the number of lines of code in a git repository?" I have another suggestion. github.com/AlDanial/cloc/

cloc --vcs=git
     531 text files.
     523 unique files.
       8 files ignored.

github.com/AlDanial/cloc v 1.96  T=0.65 s (803.0 files/s, 95519.7 lines/s)
-------------------------------------------------------------------------------
Language                     files          blank        comment           code
-------------------------------------------------------------------------------
JSON                            12              2              0          33270
Vuejs Component                245           1396             77          17896
TypeScript                     141            452            129           4645
GraphQL                         72            123             10           2692
Markdown                         2            166              0            532
SVG                             30              0              0            389
JavaScript                      11             15             14            249
HTML                             2              3              0             35
YAML                             2              4              2             24
CSS                              1              3              1             17
diff                             2              0             26             17
Dockerfile                       1              2              1             10
Bourne Shell                     2              2              0              6
-------------------------------------------------------------------------------
SUM:                           523           2168            260          59782
-------------------------------------------------------------------------------
Enter fullscreen mode Exit fullscreen mode
Collapse
 
iamandrewluca profile image
Andrei Luca

And for npx npm-check-updates -u now you can use npm update --save