Search code examples
javascriptreactjsecmascript-6systemjs

React ES6 SystemJS - Uncaught (in promise) Error: Unexpected token <(…)


I have react and react-dom installed and imported in via the System.config below, but I still get this error below:

Uncaught (in promise) Error: Unexpected token <(…)

HTML:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>ES2015 Module Example</title>
</head>
<body>
    <script src="lib/system.js"></script>
    <script>
        System.config({
            "baseURL": "src",

            // Set defaultJSExtensions to true so you don't have to use .js extension when importing the es6 module.
            "defaultJSExtensions": true,

            // 'plugin-babel' or 'traceur' or 'typescript'
            transpiler: 'traceur',

            map: {
                'react': './node_modules/react/dist/react.min.js',
                'react-dom': './node_modules/react-dom/dist/react-dom.min.js',
                'traceur': './lib/traceur.min.js',
                'plugin-babel': './lib/plugin-babel/plugin-babel.js',
                'systemjs-babel-build': './lib/plugin-babel/systemjs-babel-browser.js'
            },
        });
        System.import("app.js");
    </script>
</body>
<div id="example"></div>
</html>

app.js:

import React from 'react';
import ReactDOM from 'react-dom';

var Hello = React.createClass({
    render: function() {
        return <h1>Hello {this.props.name}</h1>;
    }
});

ReactDOM.render(
    <Hello name="World" />,
    document.getElementById('example')
);

Any ideas what else do I have to configure?


Solution

  • browserify is the best solution (for production & development) - to me:

    npm install --save-dev babel-preset-react
    

    gulp:

    var gulp = require('gulp');
    
    var browserify = require('browserify');
    var babelify = require('babelify');
    var source = require('vinyl-source-stream');
    var buffer = require('vinyl-buffer');
    var uglify = require('gulp-uglify');
    var sourcemaps = require('gulp-sourcemaps');
    var livereload = require('gulp-livereload');
    
    gulp.task('build', function() {
        // app.js is your main JS file with all your module inclusions
        return browserify({entries: 'src/app.js', debug: true})
            .transform("babelify", { presets: ["es2015", "react"] })
            .bundle()
            .pipe(source('compile.min.js'))
            .pipe(buffer())
            .pipe(sourcemaps.init())
            .pipe(uglify())
            .pipe(sourcemaps.write('./maps'))
            .pipe(gulp.dest('js'))
            .pipe(livereload());
    });
    
    gulp.task('default', ['build']);
    

    As for non-production with SystemJS (painfully slow):

    <!DOCTYPE html>
    <script src="https://jspm.io/system@0.19.js"></script>
    <script>
    System.config({
      transpiler: 'babel',
      babelOptions: {}
    });
    System.import('./main.js');
    </script>
    

    You still can use gulp for development. Just add this to the gulpfile:

    gulp.task('watch', ['build'], function () {
        livereload.listen();
        gulp.watch('js/*.js', ['build']);
    });
    
    gulp.task('default', ['watch']);
    

    This saves you from other tedious dev workflows as listed here.