Commander.js: How to use options with babel-node bin file?

Created on 19 Jan 2016  路  9Comments  路  Source: tj/commander.js

myfile.js

#!/usr/bin/env babel-node

// commander demo
var program = require('commander');

program
    .version('0.0.1')
    .option('-p, --peppers', 'Add peppers')
    .option('-P, --pineapple', 'Add pineapple')
    .option('-b, --bbq-sauce', 'Add bbq sauce')
    .option('-c, --cheese [type]', 'Add the specified type of cheese [marble]', 'marble')
    .parse(process.argv);

console.log('you ordered a pizza with:');
if (program.peppers) console.log('  - peppers');
if (program.pineapple) console.log('  - pineapple');
if (program.bbqSauce) console.log('  - bbq');
console.log('  - %s cheese', program.cheese);

$ ./myfile.js -p -c gauda
cause an error:

/var/scripts/node_modules/babel-cli/lib/_babel-node.js:93
  code = code.trim();
              ^

TypeError: code.trim is not a function
    at _eval (/var/scripts/node_modules/babel-cli/lib/_babel-node.js:93:15)
    at Object.<anonymous> (/var/scripts/node_modules/babel-cli/lib/_babel-node.js:122:16)
    at Module._compile (module.js:435:26)
    at Object.Module._extensions..js (module.js:442:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:313:12)
    at Function.Module.runMain (module.js:467:10)
    at startup (node.js:136:18)
    at node.js:963:3

with --pepper instead of -p it works.

Commander 2.9.0

Most helpful comment

@rottmann I found that changing the first line to the following in my main command and all subcommands seems to work:

#!/usr/bin/env babel-node --

Does that work for you?

All 9 comments

I have the same issue except the I'm using -e. When using -e it seems that node is being called with _its_ -e flag. If I provide the long argument --environment, I don't get an error.

@rottmann I found that changing the first line to the following in my main command and all subcommands seems to work:

#!/usr/bin/env babel-node --

Does that work for you?

Haven't tried this. Thanks @euoia

@zhiyelee can we close this?

@euoia with -- i got /usr/bin/env: babel-node --: No such file or directory

Have you tried installing babel-cli globally?

npm install -g babel-cli

Seems to work now.

I tried the above, but it is still not working. I tried installing babel-cli globally, but still getting ES6 syntax errors . Is there something wrong with bin/env ? where the babel-node suppose to be present?

I tried the above, but it is still not working. I tried installing babel-cli globally, but still getting ES6 syntax errors . Is there something wrong with bin/env ? where the babel-node suppose to be present?

I resolved the issue. So basically, my npm start script was"start": "babel-node --presets es2015 index.js"
And I had only put #!/usr/bin/env babel-node at the start of my js file. And I was trying to it via cli. But I for got to create a .babelrc file with preset -

{
  "presets": ["babel-preset-es2015"]
}
Was this page helpful?
0 / 5 - 0 ratings