Javascript 我可以在 node.js 中使用 CoffeeScript 而不是 JS 吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4679782/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can I use CoffeeScript instead of JS for node.js?
提问by donald
What are my restrictions if I want to code node.js and use CoffeeScript? Can I do anything I'd be able to do in JS?
如果我想编写 node.js 并使用 CoffeeScript,我有什么限制?我可以做任何我可以在 JS 中做的事情吗?
采纳答案by David Tang
Yes, CoffeeScript simply compiles into pure JS, making it completely compatible with node.js.
是的,CoffeeScript 只是简单地编译成纯 JS,使其与 node.js 完全兼容。
To run CoffeeScripts on node, you can either:
要在节点上运行 CoffeeScripts,您可以:
- Type
coffee -c example.coffee
to compile, followed bynode example.js
to run the compiled JS. - Simply type
coffee example.coffee
- 类型
coffee -c example.coffee
来编译,然后node example.js
运行编译的JS。 - 只需输入
coffee example.coffee
回答by Trevor Burnham
Not only can you run CoffeeScript files directly in Node with
你不仅可以直接在 Node 中运行 CoffeeScript 文件
coffee source.coffee
you can also require them as if they were JavaScript files. For instance, if you have lib.coffee
in a directory, you can write
你也可以像 JavaScript 文件一样要求它们。例如,如果你有lib.coffee
一个目录,你可以写
require './lib'
from another CoffeeScript file in the same directory. (In order to do this from a JavaScript file, you'll have to add require 'coffee-script'
at the top.) So, you never have to do compilation explicitly under Node, unless you're packaging your project for deployment with a tool like npm.
来自同一目录中的另一个 CoffeeScript 文件。(为了从 JavaScript 文件执行此操作,您必须require 'coffee-script'
在顶部添加。)因此,您永远不必在 Node 下显式进行编译,除非您使用 npm 之类的工具打包要部署的项目。
One caveat: In stack traces, the line numbers you'll see refer to the compiled JavaScript, even when you're running CoffeeScript directly (so you don't have access to the JavaScript). A lot of folks are trying to fix this, but it's a big challenge.
一个警告:在堆栈跟踪中,您将看到的行号是指已编译的 JavaScript,即使您直接运行 CoffeeScript(因此您无法访问 JavaScript)。很多人都在试图解决这个问题,但这是一个很大的挑战。
回答by gprasant
Yes, here's a different & simpler answer. You need to do 2 steps.
是的,这是一个不同且更简单的答案。您需要执行 2 个步骤。
npm install coffee-script --save # I assume you would have done this already
.Have
require('coffee-script')
as the first line that would get executed inserver.js
ofapp.js
. (UPDATE:since coffee script 1.7, you will have to dorequire('coffee-script/register'))
npm install coffee-script --save # I assume you would have done this already
.有
require('coffee-script')
因为这将在得到执行第一线server.js
的app.js
。(更新:从咖啡脚本 1.7 开始,你将不得不做require('coffee-script/register'))
This registers coffeescript compiler to your app and you can start treating coffee files and js files equally now (meaning that you can require coffee files too !).
这会将 coffeescript 编译器注册到您的应用程序,您现在可以开始同等对待咖啡文件和 js 文件(这意味着您也可以需要咖啡文件!)。
This method will require you to write just the one file (app.js) in vanilla javascript. But the advantage is that your deploy environment need not have coffeescript as an initial globally installed dependency to run your app. In this case, you would just have to copy over your code, and npm install
would install all packages necessary. And npm start
would have you up and running
此方法将要求您仅使用 vanilla javascript 编写一个文件 (app.js)。但优点是您的部署环境不需要将 coffeescript 作为初始全局安装的依赖项来运行您的应用程序。在这种情况下,您只需复制您的代码,并npm install
安装所有必要的包。并且npm start
让你启动并运行
回答by Mark Essel
Video Tutorials
视频教程
I've seen a great tutorial series by Pedro Teixeira. He's been building an entire series on node tutorials. He includes reference to nodemon for auto detection and compilation and reloading of edited .coffee files.
我看过Pedro Teixeira 的一个很棒的教程系列。他一直在构建关于节点教程的整个系列。他包括对 nodemon 的引用,用于自动检测和编译以及重新加载已编辑的 .coffee 文件。
回答by Roger Garzon Nieto
You can use Jitter, a Simple continuous compilation for CoffeeScript.
您可以使用Jitter,这是 CoffeeScript 的简单连续编译。
npm install -g jitter
Let's say you have a bunch of *.coffee files in the coffee directory, and want to compile them to the js directory. Then run:
假设您在coffee 目录中有一堆*.coffee 文件,并且想要将它们编译到js 目录中。然后运行:
jitter coffee js
Jitter runs in the background until you terminate it (Ctrl+C), watching for new changes.
抖动在后台运行,直到您终止它 (Ctrl+C),观察新的变化。
回答by Shripad Krishna
Coffeescript + ExpressJS + Couchdb + Redis + Auth:
Coffeescript + ExpressJS + Couchdb + Redis + 身份验证:
回答by Dave Dopson
Try this
尝试这个
#!/usr/bin/env coffee
v = 78
console.log "The value of v is '#{v}'"
Then do:
然后做:
chmod +x demo.coffee
./demo.coffee
CoffeeScript has pretty solid integration with node. Once the 'coffee-script' module is loaded, either by require('coffee-script')
, by the she-bang I demo'd above, or by running coffee demo.coffee
... once loaded, you can used require('./foo')
to bring in foo.coffee
CoffeeScript 与 node 的集成非常可靠。一旦'coffee-script'模块被加载,或者通过require('coffee-script')
我上面演示的she-bang,或者通过运行coffee demo.coffee
...一旦加载,你可以用来require('./foo')
引入foo.coffee
回答by Taner Topal
If you want to automatically compile all your coffeescript files (in one directory including subdir) every time they change into javascript, just use this command:
如果您想在每次更改为 javascript 时自动编译所有 coffeescript 文件(在一个目录中,包括 subdir),只需使用以下命令:
find . -name '*.coffee' -type f -print0 | xargs -0 coffee -wc