Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/oracle/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在javascript中编译coffeescript代码字符串?_Javascript_Node.js_Coffeescript - Fatal编程技术网

如何在javascript中编译coffeescript代码字符串?

如何在javascript中编译coffeescript代码字符串?,javascript,node.js,coffeescript,Javascript,Node.js,Coffeescript,假设我在nodejs上的javascript文件中有一个coffeescript代码字符串。如果不使用终端,如何将此字符串转换为javascript?我已经试过了,但它给了我一个关于闭合套接字的错误。我在全球安装了coffeescript,在本地安装了coffeescript编译器 编辑:以下是代码: var Compiler = require('coffeescript-compiler'); var cc = new Compiler(); cc.compile('a = 5', fun

假设我在nodejs上的javascript文件中有一个coffeescript代码字符串。如果不使用终端,如何将此字符串转换为javascript?我已经试过了,但它给了我一个关于闭合套接字的错误。我在全球安装了coffeescript,在本地安装了coffeescript编译器

编辑:以下是代码:

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
    }
});
下面是它抛出的错误:

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)
events.js:72
投掷器//未处理的“错误”事件
错误:此套接字已关闭。
在套接字处写入(net.js:637:19)
在doWrite(_stream_writable.js:225:10)
在writeOrBuffer(_stream_writable.js:215:5)
在Socket.Writable.write(_stream_Writable.js:182:11)
at Socket.write(net.js:615:40)
在docomfile(D:\TSA\App\node\u modules\coffeescript compiler\compiler.js:33:15)
在Compiler.compile(D:\TSA\App\node\u modules\coffeescript Compiler\Compiler.js:46:3)
反对。(D:\TSA\App\coffeescript.js:4:4)
在模块处编译(Module.js:456:26)
在Object.Module.\u extensions..js(Module.js:474:10)
您可以使用这个


或者,CoffeeScript()主页有一个选项卡:“Try CoffeeScript”,您可以在其中粘贴您的CoffeeScript代码,并在JavaScript中查看其等效代码。

CoffeeScript包本身提供了一个编译器功能,因此对于像您这样的简单用例,它甚至比使用
coffeecompiler
更容易。您可以通过以下方式获得您想要的:

var CoffeeScript = require('coffee-script');
var compiledJS = CoffeeScript.compile('a = 5');

CoffeeScript编译器将返回一个常规javascript字符串,但您需要通过其他方式运行该字符串

// 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;
}

不,我指的是一个类似于我可以调用来编译coffeescript的函数,比如:
var code=compile('a=“foo”)
,其中code等于
'var a=“foo”
。你能展示一下你用于
coffeescript编译器的代码吗?这绝对是值得使用的东西。