Skip to content

Instantly share code, notes, and snippets.

@neekey
Created March 9, 2013 08:31
Show Gist options
  • Save neekey/5123493 to your computer and use it in GitHub Desktop.
Save neekey/5123493 to your computer and use it in GitHub Desktop.
在Meteor中使用node.js的模块系统

这是Meteor的大坑,希望以后这块能做的更好。

首先,在Meteor中,由于经过了沙箱的封装,无法直接触碰到require, module等全局变量。

且,Meteor会自动执行所有不在testpublic目录下的js文件,因此我们需要使用的npm模块都需要安装到public目录下。

然后使用meteor-node-module进行模块的加载。

举例,我们现在需要使用mustache,那么cdpublic目录下,安装之npm install mustache,然后在我们的代码中使用:

var Mustache = NodeModules.require( 'mustache' );

来引入模块。


上面没有提到自定义模块。是的,因为meteor根本没有把module等变量暴露出来,因此目前来说模块按照文件分离的话,只能通过全局变量了,比如:

在a.js文件中定义:

Myapp = {};

在b.js文件中定义:

Myapp.foo = function(){ ... }

在c.js中调用:

Myapp.foo();

嗯...感觉回到了以前的前端脚本是么...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment