Sunday, June 2, 2024
 Popular · Latest · Hot · Upcoming
185
rated 0 times [  190] [ 5]  / answers: 1 / hits: 108638  / 9 Years ago, sat, february 28, 2015, 12:00:00

I use the require hook of BabelJS (formerly named 6to5) to run node apps with es6features:



// run.js
require(babel/register);
require(./app.js6);


I call node run.js to run my app.js6. I need to install BabelJS and provide a run.js for each project I'd like to use es6features. I would prefer a call like nodejs6 app.js6. How can I achieve this system independently (Unix and Windows)?


More From » node.js

 Answers
1

Add the babel-cli and babel-preset-es2015 (aka ES6) dependencies to your app's package.json file and define a start script:



{
dependencies: {
babel-cli: ^6.0.0,
babel-preset-es2015: ^6.0.0
},
scripts: {
start: babel-node --presets es2015 app.js
}
}


Then you can simply execute the following command to run your app:



npm start


If you ever decide to stop using Babel (e.g. once Node.js supports all ES6 features), you can just remove it from package.json:



{
dependencies: {},
scripts: {
start: node app.js
}
}


One benefit of this is that the command to run your app remains the same, which helps if you are working with other developers.


[#67627] Thursday, February 26, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
serena

Total Points: 488
Total Questions: 125
Total Answers: 114

Location: Estonia
Member since Wed, Jun 8, 2022
2 Years ago
;