I have been developing a site using babel-node and browserify with the babelify transform, to support ES6 syntax.
I am just wondering, can I run this in production as babel-node server
rather than node server
What other options do I have to run ES6 in node?
Here are the commands I am running for build and start in development
// npm run build
browserify -t [babelify] client.js > public/js/bundle.js",
// npm start
babel-node server.js"
Here are my dev dependencies
"babel": "^4.0.1",
"babelify": "^5.0.3",
"browserify": "^8.0.3"
For the client side code, you're doing the correct thing.
babelify
it and ship it to the client.For the server side code, I would just do a regular build using babel-cli
for Babel 6+
As of Babel 6, no transformations are included by default. So let's start by installing
babel-cli
andbabel-preset-es2015
.Add a transformation to your
.babelrc
file — this is the prest module we downloaded above. Take a look at the full list of presets to see which one(s) are a best fit for you.Add a
build
script to yourpackage.json
. Belowsrc
is your input files andbuild
is the transformed output filesThen build it!
Then run your code. At this point, you'll want to be executing the files in your
build
directoryfor Babel <= 5, just use the require hook.
You will be able to keep your source files in ES6 but still execute them using
node server.js
According to your comments, you seem to be having a little trouble. Pay particular attention to the yellow highlighted part above. Your first file can only be ES5, which is run by node itself. All subsequent requires will be transformed by Babel...
Here's what a typical setup might look like
server.js
app.js
fire it up!