The main goal of nodejsscript
is to make “one-file” scripting easy
and mainly quick and cross-platform. In a lots of cases the nodejsscript
is just an ‘extension’ of the node
-js runtime.
Almost everything can be done with just “pure” JavaScript script file written
for node
. In case, you just need (“very” small) script with only basic
functionalities available in node, you can just use:
#!/usr/bin/env node
// script.js (chmod +x script.js)
const { version }= require("./package.json", "utf8");
console.log(version);
…similarly for --eval
/--print
/--interactive
:
git tag $(node -p "require('./package.json').version"); git push --tag
Also, you can use libraries used by nodejsscript
directly when you
scripting inside the package. Just install it and use it:
- shelljs - npm for Unix shell-like function in JavaScript
- sade - npm for building command-line interface (you can use also more robust solutions like commander - npm)
- css-in-console - npm for styling in console (you can use alternative solutions like chalk - npm)
There is non-promoted functionality similar to nodejsscript
in
ShellJS library. You can use shjs
to execute scripts, too.
#!/usr/bin/env -S npx shjs
echo(ls());
…keep in mind that only commonJS modules are supporteda and only functions shipped with ShellJS are available.
In this case, the npm package should be used instead of the script file. Follow for example this guide Publishing a Node.js CLI tool to npm in less than 15 minutes | HackerNoon.
In the package, you can use nodejsscript
or if it makes sence
(for example because size/performance optimization) to use libraries
directly.
In future version there can be implemented some migration helper see Candidate to implement — (interactive) cli migration to package.