I'm trying to import Coffeescript v2 module from Node .js-file. I have installed coffeescript v2 (coffeescript@next
), but I can't get export/import working between coffeescript and javascript files.
My code is:
// index.js
require("coffeescript/register")
require('babel-register')
require("./server.js")
// server.js
import Foo from "./example.coffee"
// example.coffee
console.log("qwe")
console.log "abc"
export default a = 2
node index.js
fails with
.../example.coffee:7
export default a = 2;
^^^^^^
SyntaxError: Unexpected token export
at createScript (vm.js:74:10)
at Object.runInThisContext (vm.js:116:10)
at Module._compile (module.js:533:28)
at Object.loadFile (/home/petr/mern-informatics-test/node_modules/coffeescript/lib/coffeescript/register.js:16:19)
at Module.load (/home/petr/mern-informatics-test/node_modules/coffeescript/lib/coffeescript/register.js:45:36)
<...>
If I comment out export default a = 2
, then the code is working as expected (printing two lines on console), so basic coffeescript works.
If I run coffee -c example.coffee
, I get example.js
file with following contents:
// Generated by CoffeeScript 2.0.0-beta2
var a;
console.log("qwe");
console.log("abc");
export default a = 2;
which is valid javascript, so that does not seem to be a problem with core coffeescript.
Also, if I replace import Foo from "./example.coffee"
with import Foo from "./example.js"
, it also works.
So why export
in example.coffee
does not work when import
ed into server.js
, and how can I fix this?
A separate strange thing is that if I try to import example.coffee
directly from index.js
, it fails with
import Foo from "./example.coffee"
^^^^^^
SyntaxError: Unexpected token import