可以在 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/