2

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 imported 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
Petr
  • 9,812
  • 1
  • 28
  • 52
  • Are you sure the name of the Node.js module you're using is `coffescript@next`, not `coffeescript@next`? – yeputons Jun 25 '17 at 17:04
  • What happens if you replace your `.coffee` file with a corresponding `.js` file (output of `coffee`)? – yeputons Jun 25 '17 at 17:10
  • @yeputons, fixed typo, thanks. As for the your second commnt, I already write that "if I replace `import Foo from "./example.coffee"` with `import Foo from "./example.js"`, it also works." – Petr Jun 25 '17 at 17:20
  • What node and V8 versions do you run? – yeputons Jun 25 '17 at 17:33
  • @yeputons, node v8.1.2; I don't know how to find version of V8 (node-v8-version fails with "8.1.2 doesn't appear to be a valid node version"). – Petr Jun 25 '17 at 17:40
  • Doesn't Coffeescript just output ES6 module syntax? That means you'd have to load it along with another loader like Babel to convert the ES6 syntax to syntax Node knows how to handle. – loganfsmyth Jun 25 '17 at 20:56
  • @loganfsmyth, I believe I already do it by writing `require("coffeescript/register"); require('babel-register')`. – Petr Jun 26 '17 at 04:26
  • Have you configured Babel? If not, then this is a dup of https://stackoverflow.com/questions/33440405/babel-file-is-copied-without-being-transformed – loganfsmyth Jun 26 '17 at 18:05
  • @loganfsmyth, I have tried configuring babel via `require('babel-register'){"presets": ["env"]});` and via `.babelrc` (though the latter does not make sense for me), but the problem persists. Also it is running OK when I import the .js-file that is generated by coffeescript, so it does not seem to be a babel configuration problem. – Petr Jun 26 '17 at 19:37
  • You'll need to set `extensions: [".coffee", ".js"]` but it's also possible that it won't work even with that now that I think about it. I'm not sure `babel-register` handles chaining register hooks right now. You might be better off using a precompilation pipeline with Gulp or something. – loganfsmyth Jun 26 '17 at 21:01
  • @loganfsmyth, ` I'm not sure babel-register handles chaining register hooks right now. ` -- yes, this seems to be the reason. – Petr Jun 30 '17 at 06:24
  • Using register hooks isn't great for a production system, you might be best off putting together a build pipeline with something like Gulp. That is a very common workflow. – loganfsmyth Jun 30 '17 at 06:25
  • @loganfsmyth, yes, I understand this, but I was looking for a solution for a development system. – Petr Jun 30 '17 at 07:38
  • 1
    Using a Gulp watcher is also very common for local development. – loganfsmyth Jun 30 '17 at 16:56

0 Answers0