
Security News
CISA Kills Off RSS Feeds for KEVs and Cyber Alerts
CISA is discontinuing official RSS support for KEV and cybersecurity alerts, shifting updates to email and social media, disrupting automation workflows.
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
The npm package runex receives a total of 51 weekly downloads. As such, runex popularity was classified as not popular.
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
CISA is discontinuing official RSS support for KEV and cybersecurity alerts, shifting updates to email and social media, disrupting automation workflows.
Security News
The MCP community is launching an official registry to standardize AI tool discovery and let agents dynamically find and install MCP servers.
Research
Security News
Socket uncovers an npm Trojan stealing crypto wallets and BullX credentials via obfuscated code and Telegram exfiltration.