nodejs posts

I don’t use npm often. When I do, I tend to be reminded of annoyances in using it. One such annoyance is that it doesn’t take into account the node version in use when installing new packages, as I posted about four years ago. It just goes with the latest if no version is specified, regardless of its ability to run in the available environment. So I have to manually step back versions until I find one that works. The alternative package manager yarn at least has the good grace to error out for incompatible versions, so I used that and just decremented the version of each package until it stopped erroring.

Some of us like to let our OS package manager manage our OS-wide software for security, stability, and simplicity, and that isn’t always the latest and greatest.


I found myself needing to get the path to the current script and its directory in a local Node JS script recently. In Common JS scripts, that is available by __filename or __dirname globals, but it isn’t available by the same means in ES modules. Instead, there is import.meta.url, which can be used to get at the directory name if needed, like:

const __filename = import.meta.url.replace(/^file:[\/]+/, '/');
const __dirname = __filename.split('/').slice(0, -1).join('/');

</toby>