简体   繁体   English

安装帮助程序文件的节点模块在安装时导致“找不到模块”错误

[英]Node module with Helper file results in 'cannot find module' error when installed

Apologies in advance, I'm sure there's a simple answer in an obvious thread I've just failed to find it/understand it. 预先道歉,我敢肯定在一个显而易见的线程中有一个简单的答案,我只是未能找到/理解它。

Given a node.js module Jimmy implemented in the global node_modules/Jimmy/index.js file; 给定在全局node_modules/Jimmy/index.js文件中实现的Jimmy的node.js模块; I have a few 'helper' functions that I put in a separate Helper.js file, all in the same directory. 我有一些“帮助程序”功能,我将它们放在单独的Helper.js文件中,它们都位于同一目录中。 Works great when I run a sample program require ing it from the same directory, but running that same sample program from anywhere else, it fails to load the supporting Helper.js file: 当我运行一个示例程序require在同一目录中运行该示例程序时,效果很好,但是在其他任何地方运行该示例程序时,都无法加载支持的Helper.js文件:

    Error: Cannot find module 'Helper'

Can someone explain or point me to an explanation of why this doesn't work and what I should be doing to make it work? 有人可以向我解释或指出为什么不起作用以及如何使它起作用的解释吗?

Edit: Dumbed it down to just demo the problem. 编辑:哑巴下来只是演示问题。 Including the Swagger-client dependency just in case that matters. 包括Swagger-client依赖关系,以防万一。

Node Module is called Jimmy in the directory ~/Jimmy . 节点模块在~/Jimmy目录中称为~/Jimmy

Module class defined in ~/Jimmy/index.js : ~/Jimmy/index.js定义的模块类:

var Swagger = require('swagger-client');
var Helper  = require('Helper');
function Jimmy (host, username, password) {
    this._host = host;
    this._hdr = {
        'Authorization': 'Basic ' + new     Buffer(username+':'+password).toString('base64') ,
        'Content-Type': 'application/json'
    };
}
Jimmy.prototype.getEntity = function(entity) {
    return Helper.get(this._host, this._hdr, entity);
}
module.exports =
{
    Jimmy: Jimmy
};

Module helper function defined in ~/Jimmy/Helper.js : ~/Jimmy/Helper.js定义的模块帮助器函数:

var Swagger = require('swagger-client');
function get(host, hdr, entity) {
    var url = 'http://'+host+'/config/' + entity;
    var request = {
        url    : url,
        headers: hdr
    };
    return Swagger.http(request)
        .then( (res) => { return res.body; });
}
module.exports =
{
    get : get
};

And sample test in ~/test.js : 并在~/test.js示例测试:

var jimmy = require('Jimmy');
var j = new jimmy.Jimmy('192.168.56.151:8080', 'admin', 'admin');
j.getEntity('joey')
    .then( (e) => {
        console.log(JSON.stringify(e, null, 2));
    })
    .catch( (err) => { console.log('ERR: ' + err); });

Sorry, found my mistake; 对不起,发现我的错误; in case anybody else makes this, for a local file within the module I made it work by referencing it via a relative pathname. 万一其他人做到了,对于模​​块中的本地文件,我通过相对路径名引用来使其起作用。

So in my case where from my module's index.js I only wanted the local version of Helper.js I wanted: 因此,在我的情况下,从模块的index.js我只想要本地版本的Helper.js

var Helper  = require('./Helper'); // works

rather than: 而不是:

var Helper  = require('Helper'); // 'not found' error

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM