Security News
The Push to Ban Ransom Payments Is Gaining Momentum
Ransomware costs victims an estimated $30 billion per year and has gotten so out of control that global support for banning payments is gaining momentum.
Readme
Run module export as a node
or npx
script.
(See Why not ... for alternative approaches.)
So you have some code that you want to be able to run from the command line.
You can of course just write it down into a file and run it with node ./script.js
.
Maybe you go one more step and add a hashbang and make it executable,
so on a linux shell you run it with just ./script.js
.
But this way you can not import the file without executing all the code.
Wrapping all the code into a function and executing it if (require.main === module)
helps with that.
You also manage to parse those arguments you need, maybe using one of the available libraries.
try {...} catch (err) {...}
or .then(...).catch(...)
?Don't regret, runex
was created, because this was just to much time and code
for something that convenient.
async
functions/promisesAs soon as your module exports a method with the name run
, it is "runnable":
Usage: [npx] runex [options] runnable [args]
Options:
-r, --require <module> 0..n modules for node to require (default: [])
-h, --help output usage information
async
/ return a Promise
stdout
(runex
only communicates over stderr
), with one exception:
stdout
(so you don't have to, see goal)
if typeof value === 'object
it will be JSON.stringify
ed.Go check some examples.
Of course your can make use of runex
to make your module "executable"
if (require.main === module) {
require('runex').run(module);
}
The above code would make the following differences:
node [path/to/]module
instead of node runex [path/to/]module
https://github.com/DVLP/run-func:
This library was my initial inspiration when searching for an existing solution.
But I think not every function is easy to run as a script
(just think about the type of the arguments, return types, etc.).
So I decided to rely on a naming convention for the executable function.
if (require.main === module) {...}
:
The "native" way of making a module "executable".
But the tricky part is what needs to be written instead of ...
, in every "executable" module.
That's why I listed the first first goal.
BTW: runex
makes use of this mechanism, so you don't need to, but you can of course have both.
Do you know an alternative that should be listed? Create an issue or PR!
FAQs
Run module export as a script
We found that runex demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Ransomware costs victims an estimated $30 billion per year and has gotten so out of control that global support for banning payments is gaining momentum.
Application Security
New SEC disclosure rules aim to enforce timely cyber incident reporting, but fear of job loss and inadequate resources lead to significant underreporting.
Security News
The Python Software Foundation has secured a 5-year sponsorship from Fastly that supports PSF's activities and events, most notably the security and reliability of the Python Package Index (PyPI).