1

So let's say I have a string of coffeescript code, in a javascript file on nodejs. How could I convert this string into javascript, without using the terminal? I have tried coffeescript-compiler, but it gave me an error about a closed socket. I have coffeescript installed globally and coffee-script compiler installed locally.

Edit: Here is the code:

var Compiler = require('coffeescript-compiler');
var cc = new Compiler();

cc.compile('a = 5', function (status, output) {
    if (status === 0) {
        // JavaScript available as a string in the `output` variable
    }
});

And here is the error it throws:

events.js:72
    throw er; //unhandled 'error' event

Error: This socket is closed.
    at Socket._write (net.js:637:19)
    at doWrite (_stream_writable.js:225:10)
    at writeOrBuffer (_stream_writable.js:215:5)
    at Socket.Writable.write (_stream_writable.js:182:11)
    at Socket.write (net.js:615:40)
    at doCompile (D:\TSA\App\node_modules\coffeescript-compiler\Compiler.js:33:15)
    at Compiler.compile (D:\TSA\App\node_modules\coffeescript-compiler\Compiler.js:46:3)
    at Object.<anonymous> (D:\TSA\App\coffeescript.js:4:4)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
1
  • Can you show the code you use for coffeescript-compiler? It is definitely the thing to use. Commented Nov 9, 2014 at 1:42

3 Answers 3

2

The CoffeeScript package itself provides a compiler function, so for a simple use case like yours it's even easier than using coffee-compiler. You can get what you want with something like this:

var CoffeeScript = require('coffee-script');
var compiledJS = CoffeeScript.compile('a = 5');
Sign up to request clarification or add additional context in comments.

Comments

0

The CoffeeScript compiler will return you a regular javascript string, but then you need to run that through something else to require it.

// First the coffee-script string needs to be converted to valid javascript
function requireCoffeeScript(src, filename) {
  var script = require("coffee-script").compile(src, {filename});
  return requireJSFromString(script, filename);
}

// Now the valid javascript string can be 'required' and the exports returned
function requireJSFromString(src, filename) {
  var m = new module.constructor();
  m.paths = module.paths;
  m._compile(src, filename);
  return m.exports;
}

Comments

-1

You could use this online converter.

Or, the CoffeeScript(http://coffeescript.org/) homepage has a tab: "Try CoffeeScript", where you could paste your CoffeeScript code and see its equivalent in JavaScript.

1 Comment

No, I mean something like a function I can call to compile coffeescript, like: var code = compile('a="foo"'), where code would equal 'var a = "foo"'.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.