如何使用与 Meteor 具有传递依赖关系的 node.js 库? [英] How to use node.js libraries that have transitive dependencies with Meteor?
问题描述
可以在 Meteor 中使用 node.js 包作为 此处描述,但是由于 require
不是全局定义的,具有传递依赖关系的包(例如 xml2js 或 aws-lib)与
It is possible to use node.js packages within Meteor as described here, however as require
is not defined globally, packages having transitive dependencies (as for example xml2js or aws-lib) break with
<代码>参考错误:需要未定义关于如何在不更改库的情况下修复或解决此问题的任何想法?
ReferenceError: require is not defined
Any ideas on how to fix or work around this issue without altering the libraries?
推荐答案
我按照您的 链接问题.我使用 node-xml2js 库通过 来自代码库的测试装置 并通过以下方式实现.
I followed the instructions from your linked question. I used the node-xml2js library to test this with the test fixture from the code base and achieved it in the following way.
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 库进行任何更改.
I think the key was to define a variable require
and assign it to Meteor's require function. When Meteor loads the server assets, it also loads require
and solves the problem of the transitive dependency. I made no changes to the node-xml2js library.
希望这会有所帮助!
这篇关于如何使用与 Meteor 具有传递依赖关系的 node.js 库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!