npx

image credits

Running npx command name automatically finds the correct reference of the command inside the node_modules folder of a project, without needing to know the exact path, and without requiring the package to be installed globally and in the user’s path. — The npx Node.js Package Runner

Two weeks ago the first online Node.js Munich Meetup took place. During a presentation I saw npx again and it reminded me that I had wanted to try it for a while.

Sometime in the past I had already seen it, but at that time I had no use for it and I forgot about it. Today I saw npx again and thought that I just need to have a closer look 🧐 at it.

Running node tools and scripts

I don’t like to install nodejs packages globally, because I don’t know which node version will work and what dependencies they need, which are then globally installed. For this reason I have installed the global tools locally only. To run them I have called ./node_modules/.bin/mocha. Another possibility is to put the ./node_modules/.bin directory into the PATH variable, but only for the specific project. For this purpose direnv is very suitable, because direnv automatically sets the environment variables as soon as you change into the directory.

npx

npx automatically finds the correct reference of the command within the node_modules folder of a project. It is not necessary to know the exact path. Likewise, the package does not need to be global or in the user’s path.

If you want to know more, have a look at the article Introducing npx: an npm package runner. It gives a wonderful overview.

Summary

I finally looked at npx. Now I should not forget it. I also replaced the ./node_modules/.bin calls of my sap-leonardo npm package with npx calls.