【问题标题】:How can I use NodeJS modules in Meteor?如何在 Meteor 中使用 NodeJS 模块?
【发布时间】:2013-02-08 23:11:43
【问题描述】:

在一个 NodeJS 应用程序中,我已经完成了一些模块,现在我想在 Meteor 中使用它们,我该怎么办? 比如有个文件'hello.js',内容:

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

如何在流星中使用“say Hi”?

当我这样做时:

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

错误是:

应用程序/index.js:1 要求(); ^ ReferenceError:未定义要求 在 app/index.js:1:1 在/home/huyinghuan/workspace/NodeJs/myMeteorJS/testrequire/.meteor/local/build/server/server.js:113:21 在 Array.forEach (本机) 在 Function._.each._.forEach (/usr/lib/meteor/lib/node_modules/underscore/underscore.js:79:11) 在运行时(/home/huyinghuan/workspace/NodeJs/myMeteorJS/testrequire/.meteor/local/build/server/server.js:99:7)

【问题讨论】:

    标签: meteor


    【解决方案1】:

    我认为,您必须将您的模块安装/复制到projectdir/.meteor/local/build/server/node_modules,这是指向/usr/local/meteor/lib/node_modules 的链接。我用 node.js 模块tracer 尝试了这个,它成功了。每次更新流星安装时,都必须将文件复制到此目录中。

    【讨论】:

    • 感谢您的帮助。根据您的提示,我在流星中成功使用了我的moudles。
    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        【解决方案4】:

        这是一个让在 Meteor 中使用 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;
            }
          });
        }
        

        【讨论】:

          猜你喜欢
          • 2017-10-27
          • 2017-09-02
          • 2020-01-02
          • 2012-05-17
          • 2012-02-20
          • 2020-08-06
          • 2019-10-21
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多