How to use node.js libraries that have transitive dependencies with Meteor?

谁说我不能喝 提交于 2019-12-21 05:14:15

问题


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?


回答1:


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');
        });
    });
});

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.

Hope this helps!




回答2:


In the latest version of Meteor (0.6.3, and presumably onwards), you need to use Npm.require() instead of require() by itself.



来源:https://stackoverflow.com/questions/11980356/how-to-use-node-js-libraries-that-have-transitive-dependencies-with-meteor

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!