node.js - 如何使用与 Meteor 具有传递依赖关系的 node.js 库?

标签 node.js meteor

可以在 Meteor 中使用 node.js 包作为 described here ,然而,由于 require 没有全局定义,具有传递依赖性的包(例如 xml2js 或 aws-lib)与

ReferenceError:未定义要求 关于如何在不更改库的情况下解决或解决此问题的任何想法?

最佳答案

我按照您的说明操作 linked question .我使用 node-xml2js 库通过 test fixture from the code base 来测试它并通过以下方式实现。

Meteor.startup(function () {

    // This solves the issue
    var require = __meteor_bootstrap__.require;

    // The example from node-xml2js readme
    var fs = require('fs'),
        xml2js = require('xml2js');

    var parser = new xml2js.Parser();
    fs.readFile('/home/prashant/order.xml', 'utf8', function(err, data) {
        parser.parseString(data, function (err, result) {
            console.log(result);
            console.log('Done');
        });
    });
});

我认为关键是定义一个变量 require 并将其分配给 Meteor 的 require 函数。 Meteor 在加载服务端资源的同时加载了require,解决了传递依赖的问题。我没有对 node-xml2js 库进行任何更改。

希望这对您有所帮助!

关于node.js - 如何使用与 Meteor 具有传递依赖关系的 node.js 库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11980356/

相关文章:

javascript - 为什么 Chrome 在 "strict mode"中使用 block 内的函数时仍然保持沉默?

javascript - 从meteor中的JSONP中提取数据

angular - 连接为 Meteor 客户端的 Ionic UI 的简单示例?

Azure 网站 500 上的 Node.js 状态

node.js - 重启node.js集群worker

node.js - 使用quotaUser后,userRateLimitExceeded错误谷歌驱动器API

meteor - 为什么新应用程序会出现 'Uncaught ReferenceError: Template is not defined' 错误

node.js - Node js快速路由所有路径

meteor - 在 meteor 中使用参数进行订阅

没有 'www' 前缀, meteor 链接不起作用