Search code examples
javascriptcoffeescriptbrowserifyes6-module-loader

Use ES6 import with CoffeeScript and Browserify


CoffeeScript doesn't support ES6 import yet. (See https://github.com/jashkenas/coffeescript/issues/3162)

I tried to use the following syntax as explained in the link:

`import { createAction, handleAction, handleActions } from 'redux-actions'`

But Browserify throws the following error:

Browerify { err: 
   { [Error: Parsing file: 'import' and 'export' may only appear at the top level (2:0)]

So, I'm currently stuck with the old traditional way:

reduxActions = require 'redux-actions'
console.log reduxActions.createAction

Which works fine but force me to either use reduxActions.createAction or to manually define createAction = reduxActions.createAction in each file were I need it. I'm looking for a better way of doing this, if any.


Solution

  • A collegue of mine found a solution:

    { createAction, handleAction, handleActions } = require 'redux-actions'

    It's the best way of doing this I've found so far, looks like ES6.