2013-02-23 230 views
1

在NodeJS应用程序中,我完成了一些模块,现在我想在流星中使用它们,我该怎么做? 例如,有一个文件“hello.js”,内容:如何在Meteor中使用NodeJS模块?

require('url');// In here,require other modules 
function sayHi(name){ 
     console.log("Hi "+ name); 
} 
exports.sayHi = sayHi; 

如何使用流星“打招呼”?

当我这样做:

if (Meteor.isServer) { 
     Meteor.startup(function() { 
     var require = __meteor_bootstrap__.require; 
     var index = require('./hello'); 
     hello.syaHi('Ec');})} 

错误是:

 

app/index.js:1 
require(); 
^ 
ReferenceError: require is not defined 
    at app/index.js:1:1 
    at /home/huyinghuan/workspace/NodeJs/myMeteorJS/testrequire/.meteor/local/build/server/server.js:113:21 
    at Array.forEach (native) 
    at Function._.each._.forEach (/usr/lib/meteor/lib/node_modules/underscore/underscore.js:79:11) 
    at run (/home/huyinghuan/workspace/NodeJs/myMeteorJS/testrequire/.meteor/local/build/server/server.js:99:7)

回答

2

我想,您必须安装/你的模块复制到projectdir/.meteor/local/build/server/node_modules这是/usr/local/meteor/lib/node_modules的链接。我试着用node.js模块tracer,它工作。每次更新流星安装时,都必须将文件复制到此目录中。

+0

感谢您help.According到你的秘诀,我成功地在流星中使用我的模块。 – 2013-02-24 03:35:31

2

此外,它看起来像Npm.require()是现在需要节点模块的正确方法。

0

更新,我不得不将我的模块安装到.meteor/local/build/programs/server/node_modules以及使用Npm.require。

0

这里有一个包,这使得它更容易使用内流星NPM包:

https://github.com/meteorhacks/npm

用法示例:

if (Meteor.isClient) { 
    getGists = function getGists(user, callback) { 
    Meteor.call('getGists', user, callback); 
    } 
} 

if (Meteor.isServer) { 
    Meteor.methods({ 
    'getGists': function getGists(user) { 
     var GithubApi = Meteor.npmRequire('github'); 
     var github = new GithubApi({ 
      version: "3.0.0" 
     }); 

     var gists = Async.runSync(function(done) { 
     github.gists.getFromUser({user: 'arunoda'}, function(err, data) { 
      done(null, data); 
     }); 
     }); 

     return gists.result; 
    } 
    }); 
}