H
H
hbrmdc2016-03-25 13:37:00
JavaScript
hbrmdc, 2016-03-25 13:37:00

How to make NodeJS server understand es6 import and export?

I am using gulp, babelify and browserify to compile the client side:

gulp.task('buildApp', function() {
  return browserify('src/js/main.js')
    .transform(babelify)
    .bundle()
    .pipe(source('main.js')) // gives streaming vinyl file object
    .pipe(buffer()) // <----- convert from streaming to buffered vinyl file object
    .pipe(uglify()) // now gulp-uglify works 
    .pipe(gulp.dest('./dist'));
});

How to do the same for the server side? As far as I know, browserify doesn't work with server-side code.
Here it works:
gulp.task('server', () => {
  return gulp.src('server/server.js')
    .pipe(babel({
      presets: ['es2015', 'react']
    }))
    .pipe(gulp.dest('./dist'));
});

... but completely ignores import and export

Answer the question

In order to leave comments, you need to log in

3 answer(s)
P
pomeo, 2016-03-28
@pomeo

Konstantin Kitmanov and Aves from babel-register were close, only it was in babel 5, now it's different.
Create .babelrc

{
  "presets": ["es2015"],
}

Create for example index.js
require('babel-core/register');
require('./server');

In server.js, you already have a regular node, import, export, etc. they will work

A
Alexander Sharihin, 2016-03-25
@Pinsky

Node version?
If LTS then it should pick up immediately (if there is "use strict"), if it's younger, then the --harmony key is needed

K
Konstantin Kitmanov, 2016-03-25
@k12th

This is not how it should be done on the server. And how - it is written on the off. website: https://babeljs.io/docs/usage/require/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question