https://github.com/zhangchiqing/run-exported
Run an exported function of a nodejs module
https://github.com/zhangchiqing/run-exported
Last synced: 4 months ago
JSON representation
Run an exported function of a nodejs module
- Host: GitHub
- URL: https://github.com/zhangchiqing/run-exported
- Owner: zhangchiqing
- Created: 2017-06-06T21:55:27.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2017-06-07T04:59:15.000Z (about 8 years ago)
- Last Synced: 2025-01-09T11:12:17.083Z (6 months ago)
- Language: JavaScript
- Homepage:
- Size: 2.93 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
### How to install
```
npm install -g run-exported
```### How to run
Let's say we have a node module `script.js` that exports a function `main` that computes some value and print it:
```
var fib = function(n) {
if (n === 0) { return 0; }
if (n < 2) { return 1; }
return fib(n - 1) + fib(n - 2);
};exports.main = function() {
console.log(fib(10));
};
```To run the script, we will have to add a new line into the file
```
exports.main();
```Instead, we can use `run-exported` to run the exported `main` function like this:
```
run-exported script.js main
55
```### How to run with arguments
Let's say the main function now takes the number from arguments
```
var fib = function(n) {
if (n === 0) { return 0; }
if (n < 2) { return 1; }
return fib(n - 1) + fib(n - 2);
};exports.main = function() {
console.log(fib(parseInt(process.argv[2], 10)));
};
```To pass the arguments to the script, simply appending the arguments like this
```
$ run-exported script.js main 10
55
```