Categories
Terminal Tutorials

How npx / npm exec works

I was working with angular cli and wanted to run the local version of angular not the global version. The reason was to run the available angular migrations with the installed version of Angular. I reached for the following path.

./node_moduels/@angular/cli/bin/ng.js

This is long and verbose. Finding the executable in different system is task on its own.

All the executable can also be found inside

./node_modules/.bin/*

Guess what that is exactly what npx does.

npx run npm exec at the time of writing, we use npx as below

npx source-map-explorer

The above command is expanded as below. We will use npx exec from now onwards

npm exec -- source-map-explorer

This is will first check the local bin folder for the binary source-map-explorer inside ./node_modules/.bin/*

If it finds it, the local version will be executed, if not than global version is checked that is ~/.bin/*/*

If not found than it will ask to

A confirmation message by npm when asking to execute source-map-explorer via npm exec

We can suppress the confirmation by providing options like --yes -y or --no -n as following

npm exec --yes -- source-map-explorer

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.