Cross-platform node scripts: better-npm-run to the rescue

We know that Node.js is awesome and JavaScript is the ultimate cross-platform language. Node has support for Windows despite the node command being a command line program. Windows users can use the command line shell for node. But the Windows shell environment isn't completely compatible with what Unix/Linux/Mac users are used to (i.e. Bash). The npm package Better-npm-run helps deal with running tasks with node across platforms including Windows.

Get it here: https://www.npmjs.com/package/better-npm-run

This package enhances what your scripts in your package.json for your node project can call. So instead of writing complex command lines which will only run in a POSIX shell (or not, but definitely not in cmd.exe) you can specify the various command line parameters such as inline setting of environment variables (e.g. PATH=/foo/bar). Windows shell also doesn't support the conditional && to run another command on one line depending on the return value of the left-hand side command.

Unfortunately, for more advanced stuff which can't be supported just by adding configuration to package.json, they recommend creating .sh shell scripts and calling them. Thus you'll still have problems running the same code and configuration on Windows.

There are other npm packages which help you right npm scripts as configuration rather than command lines, e.g. cross-env. Using this approach, it is possible to take over the duties of Grunt/Gulp with just node scripts and packages.

Note: better-npm-run should be a devDependency and be installed on the platform where it will be run. A local relative install of better-npm-run on one platform like Linux will not then run on Windows.

Add new comment